PHP if statement breaks webpage [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 8 years ago.
Improve this question
I didn't see an answer in a quick search, so I decided make a new one, I am on the checkout_shipping.php in OScommerce 2.3.4 and I am adding an if statement so that the value of $0.00 is the text "free" here is my code, it is incomplete because it is crashing the page, here is the code that I modified.
<?php
if ('cost' > 0) {
?>
<td><?php echo $currencies->format(tep_add_tax($quotes[$i]['methods'][$j]['cost'],
(isset($quotes[$i]['tax']) ? $quotes[$i]['tax'] : 0))); ?></td>
<php
} ?>
fixed, it was syntax error; also cost wasn't a value, so I changed it to $i

Actually you are comparing 0 to a string, what you need to do is compare zero to a variable like so:
<?php
$count = 1;
if($count > 0) {
//Do your logic here
}

The first issue is your use of <php instead of <?php. But also the syntax of if ('cost' > 0) { makes no sense at all. So assuming cost is actually a variable named $cost, then this should work:
if ($cost > 0) {
?>
<td><?php echo $currencies->format(tep_add_tax($quotes[$i]['methods'][$j]['cost'],
(isset($quotes[$i]['tax']) ? $quotes[$i]['tax'] : 0))); ?></td>
<?php
} ?>

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"

I would like to print all the months inside a select tag. Can you point out where I went wrong [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 5 years ago.
Improve this question
I would like to print all the months inside a select tag. Can you point out where I went wrong
<select>
<?php
$array=array("January","February","March","April","May","June","July","August","September","October","November","December");
for($i=$array[0];$i<=$array[11];$i++)
{
echo "<option>$i</option>";
}
?>
</select>
Your loop should be
for ($i=0; $i < count($array); $i++)
{
echo "<option>$array[$i]</option>";
}
You can use below code too. I have modified your code.
<?php
$array=array("January","February","March","April","May","June","July","August","September","October","November","December");
echo '<select>';
foreach($array as $month){
echo "<option>".$month."</option>";
}
echo '</select>';
?>

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

Making two PHP echo statements do some math [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 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;

Categories