Function does not work with variable in parameter

Asked

Viewed 77 times

0

Hello,

I love programming, but I started programming recently.

Recently we decided to make a major modification on the website of the company in which I am a partner.

Everything worked fine, but I found a limitation in Learnpress, the course management plugin that we use. We would like to display the split prices below the full amount of each course registered in Learnpress.

As I did not find any add-on or something ready on the internet, I created the following Function that calculates the installment price of each course, with minimum amount of $ 9,00 and added to the file functions.php Wordpress.

I tested Function and it worked perfectly.

Function installment_price_course($nparcelas, $sale_price) {

$sale_price = (float)$sale_price;

$pparcelado = $sale_price/$nparcelas;

if ($sale_price < 9) { $nparcelas = 1; $pparcelado = $sale_price; echo $nparcelas. ' x R$ '.number_format($pparcelado, 2, ",", "."). ' interest-free'; }

Else {

if ($pparcelado >=9) { $nparcelas = 12; echo $nparcelas. ' x R$ '.number_format($pparcelado, 2, ",", "."). ' interest-free';}

Else {

for ($nparcelas = 12; $nparcelas >= 1; $nparcelas--) { $pparcelado = $sale_price/$nparcelas; if ($pparcelado >= 9) { break; } }

echo $nparcelas.' x R$ '.number_format($pparcelado, 2, ",", ".").' sem juros';

}

}

}

However, when calling Function with the parameters (12, $price) the following errors appear:

Warning: A non-numeric value encountered in /home/.../eduma-child/functions.php on line 101
1x R$
Warning: number_format() expects parameter 1 to be float, string given in /home/.../eduma-child/functions.php on line 106
sem juros

I thought it might be that the $price variable was a string. I then inserted the following snippet into Function to convert the string into float:


$sale_price = floatval($sale_price);

After inserting this excerpt the error has disappeared but the parceled value shown is 0. The strange thing is that if I replace the variable parameter with any number, it returns the parceled value correctly.

Follow the code of the course page.

//Template for Displaying price of single.

defined( 'ABSPATH' ) || Exit();

$user = Lp_global::user(); $Course = Lp_global::Course(); $class = ''; $class .= ( $Course->has_sale_price() ? ' has-origin' '; if ( $Course->is_free() { $class .= ' free-Course'; }

if ( ! $price = $Course->get_price_html() { Return; }

<div class="course-price">

    <div class="value <?php echo $class;?>">

        <?php if ( $course->has_sale_price() ) { ?>

            <span class="course-origin-price"> <?php echo $course->get_origin_price_html(); ?></span>

        <?php } ?>
        <?php echo $price; ?>
    </div>
    <span style="font-size: 14px; font-weight: normal"><?php installment_price_course (12, $price);?></span>
</div>


Updating

I also noticed that if I set a $price2 variable and assign a value to it, I can use it without problems as Function parameter. Ex.:


(installment_price_course (12, $price);

Also, the $price variable is ok and it shows the full course price normally. I tested it with echo $price and it’s ok.


Could someone help me? Is that what I’m trying to do to put a variable in the Function call incorrect?

I really need to solve this problem.

Thank you for your attention.

1 answer

1

You need to concatenate variables, functions and strings. In PHP concatenation is done with dots (.).

Ex in your first block with error:

echo $nparcelas.' x R$ '.number_format($pparcelado, 2, ",", ".").' sem juros';

• Notice that I used dots (in some places you had used comma) to join your variables to strings. • You were also adding spaces (to give white space) on the outside of the string, modified by moving inside the quotes, as in the excerpt . 'x R$ '. • I recommend (for semantic, non-mandatory reasons) that you add spaces whenever you separate parameters: Ex: ($pparcelado,2,",","." ); for ($cogenerated, 2, ",", ".");

  • 1

    Your second Warning about number_format is why you are providing an integer value (probably) when you need a float. (Ex: 9.75)

  • Thanks for the fixes, Ederson. I fixed the code on the site. Regarding the non-working of Function when using the $price variable, you would have some idea of the reason why?

  • On the second Warning, it may be integer (maybe even a string). But for this reason I added to Function the snippet to make the conversion to float. After that the errors are gone, but the Function result when I use the $price in the parameter gives zero. When I use another variable with a value set by me or put a number in the $price box, Function brings the correct result.

  • You are probably sending a comma-separated value to $price, when you expect to have a point as a decimal separator, so the value is mistaken for a string. For this reason you are having problems only through the variable and not direct value injection.

  • Good morning, Ederson. I removed the number_format and it also didn’t work. The value of $price is shown with the decimal separator ".". Could it be that for some reason this variable cannot be used in a Function?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.