Making two PHP echo statements do some math [closed] - php

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 9 years ago.
Improve this question
First of all sorry for the most likely simple question. I have two echo statements from woocommerce that I need to subtract from each other.
the first is
<?php echo $woocommerce->cart->get_discounts_before_tax(); ?>
This gives me the amount of the coupon discount.
The second is
<?php echo $woocommerce->cart->get_cart_subtotal(); ?>
This gives me the total off all things in the cart.
I need to subtract the coupon discount from the cart subtotal. So I set this up
<?php
$first_number = $woocommerce->cart->get_cart_subtotal ;
$second_number = $woocommerce->cart->get_discounts_before_tax ;
$sum_total = $first_number - $second_number;
print ($sum_total);
?>
but obviously the syntax is not right and I just get 0. Please help me with some simple math!

I guess you've missed ()
$first_number = $woocommerce->cart->get_cart_subtotal();
$second_number = $woocommerce->cart->get_discounts_before_tax();
Without parentheses, you're trying to access a property; but not calling the existent method itself.

Obviously one or both of the function returns are non-integers or 0. Find the return of each and change it to the appropriate property or function. Try this public property for the first number.
$first_number = $woocommerce->cart->subtotal;

Related

PHP elseif statement inside of a foreach loop [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 10 months ago.
Improve this question
Hello there I have a php foreach loop running that is all working except one part. I am pulling in data from a local json file. The beginning foreach call looks like this:
<?php foreach($boatslips as $boatslip): ?>
One of the json fields is: "rent_sale_or_both" : "rent" (but that field can be rent, sale, or both,)
Lower down in the code after some other fields are successfully rendered and inside the foreach loop still i'm trying the below code to render out whether a boat slip is for rent, sale or both; and wanting to then be able to style 'rent' 'sale' or 'both' with css eventually.
<?php
if ($boatslip->rent_sale_or_both == rent) {
echo "rent";
}elseif ($boatslip->rent_sale_or_both == sale) {
echo "sale";
}else {
echo "both";
}
?>
Then some more html after the elseif.
At the end of the for each loop it is closed off properly with <?php endforeach; ?>
Obviously this is probably not correct? I think I'm making it harder than it is?
Any thoughts?
You might be missing a couple of quotation marks.
$boatslip->rent_sale_or_both == "rent"
$boatslip->rent_sale_or_both == "sale"

how to add string to variable in php? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
i just need on small info i want just add '#' to one variable and and put add thing into one variable. i am adding small php code to here please suggest me.
<?php
$email34 = $row['email'];
$rem = '#gmail.com';
$trim_email = str_replace($rem ,'', $email34);
$tag_name ="#".$trim_emial.;
echo $tag_name;
?>
but i am getting only # as output;
but my out should be "#mahesh1" if any have idea about this code please help me. thank you advanced.
there is so much spelling mistakes in the variables... try below given code
<?php $email34 = $row['email'];
$rem = '#gmail.com';
$trim_email = str_replace($rem ,'', $email34);
$tag_name ="#".$trim_email;
echo $tag_name;
?>

generating number between 1 to million syntax error [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
This code
<? php
$randdnumber = rand(1, 1000000);
echo "you won ".$randnumber."points";
?>
gives me error
syntax error, unexpected '$randdnumber' (T_VARIABLE) in index.php
also how to add output of two function together something like ..
You won [$randdnumber] also you won [$randdnumber2]
is it possible ?
FIXED CODE
<?php
$randdnumber = rand(1, 1000000);
echo "you won ".$randdnumber."points";
?>
fix php tags and changed variable name
FIXED
<?php
$randnumber = rand(1, 1000000);
echo "you won ".$randnumber."points";
?>
The exact problem is your php open tag is having space and also variable name you assigned and echoed are different :
Please remove it and change it to
<?php
$randdnumber = rand(1, 1000000);
echo "you won ".$randdnumber."points";
?>

PHP console does not echo [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
Okay this really strange or I am missing something. When I run this very simmple PHP script on cmd I get the expected output which is 0. but when I uncomment the last two lines of code. . .nothing is displayed.
<?php
$test1 = 0;
echo $test1;
$test2 = 0;
#$test1_weight = 0:
#$test2_weight = 0;
?>
Is there some rule against declaring variables after an echo statement?
$test1_weight = 0:
--> change ":" to ";"
No there is no rule against declaring variables after an echo statement

Why the output total of ord got different value? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
**Update**
Fixed : http://codepad.org/6pB0WUm5
by http://stackoverflow.com/users/476/deceze
I have two codes, basically is same, but why, I got different output?
function domain_value($domain)
{
$split_domain = str_split($domain);
$ord_count = NULL;
foreach($split_domain as $key=>$value)
{
$ord_count += ord($value);
}
return $ord_count;
}
echo domain_value('abc');
and
echo ord('a')+ord('b')+ord('c');
Output
first output : 294
second output: 98
And what happens when you use:
echo ord('a')+ord('b')+ord('c');
which is probably what you intended in that second one :-)
In other words, you appear to have left the ord of the final part of the expression.
In fact, the only way you would normally get 98 would be with:
echo ('a')+ord('b')+('c')
(with ord only on the second term) so you may want to check again. If, as you seem to now indicate, you're using ord on each term, that works fine for me.
Both of them return 294 from 97 + 98 + 99, the ASCII values for a, b and c.

Categories