Page 1 of 1

easy one

Posted: Wed Sep 14, 2011 7:04 pm
by daijames
Hi I have tried searching for this answer but get too many results
can somebody correct this code for me
I want to declare the result of an else-if statement a variable
statement:

Code: Select all

<?php if ($f4>$f5) echo $date; else echo $start; ?>
php is already open so what syntax do I use ?

Code: Select all

$xx = if ($f4>$f5) echo $date; else echo $start; 

Re: easy one

Posted: Wed Sep 14, 2011 8:54 pm
by xtiano77
Do you mean a "ternary" operator?

Code: Select all

$xx = ($f4 > $f5) ? $date : $start;
or

Code: Select all

echo ($f4 > $f5) ? $date : $start;
http://php.net/manual/en/language.opera ... arison.php

Re: easy one

Posted: Wed Sep 14, 2011 9:09 pm
by daijames
the top one looks good...now I have read a bit about ternary operators (thx 2 u)
I have a field which seemingly wont take an echo the way its coded (sofware generated)
will give it a try & let you know
Greetings from Lanzarote Canary Islands 3.10am

Re: easy one

Posted: Wed Sep 14, 2011 9:16 pm
by xtiano77
Just tried the second on my server and it worked.

Code: Select all

echo ($f4 > $f5) ? $date : $start;
I did it by itself and not as part of a function or method, but it worked.

Re: easy one

Posted: Wed Sep 14, 2011 9:32 pm
by daijames
Your a star, yep I got it to work as well
its part of a date calculation as my server is 5 hours behind me
This was the full monty:

Code: Select all

$day = date('l jS F Y H:i', strtotime('+17 hour')) ."\n";
$start = date("l jS F Y", strtotime($row[10]));
$xx = ($row[10] > $day) ? $start : $day;
Thanks again