easy one

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
daijames
Forum Newbie
Posts: 10
Joined: Mon Sep 12, 2011 3:50 pm

easy one

Post 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; 
xtiano77
Forum Commoner
Posts: 72
Joined: Tue Sep 22, 2009 10:53 am
Location: Texas

Re: easy one

Post 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
daijames
Forum Newbie
Posts: 10
Joined: Mon Sep 12, 2011 3:50 pm

Re: easy one

Post 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
xtiano77
Forum Commoner
Posts: 72
Joined: Tue Sep 22, 2009 10:53 am
Location: Texas

Re: easy one

Post 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.
daijames
Forum Newbie
Posts: 10
Joined: Mon Sep 12, 2011 3:50 pm

Re: easy one

Post 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
Post Reply