driz Posted September 4, 2009 Share Posted September 4, 2009 Just experimenting with some PHP code, basically testing if a condition is true or not: <?php $year = "2009"; if($year = "1998") { echo "YES"; } else { echo "NO"; }; ?> But the echo will be YES even so $year is not the same as before! Obviously their is a syntax error making this not work, what's the problem? Thanks Link to comment Share on other sites More sharing options...
Ingolme Posted September 4, 2009 Share Posted September 4, 2009 Just experimenting with some PHP code, basically testing if a condition is true or not:<?php $year = "2009"; if($year = "1998") { echo "YES"; } else { echo "NO"; }; ?> But the echo will be YES even so $year is not the same as before! Obviously their is a syntax error making this not work, what's the problem? Thanks In programming, '=' is an assignment operator: it assigns the value that's on the right to the variable that's on the left.'==' is a comparing operator: it compares the left value and the right value and the expression returns true if they are equal. Link to comment Share on other sites More sharing options...
ulisses Posted September 4, 2009 Share Posted September 4, 2009 The $year variable is declared as a string, you must declare it as an integer. Instead of $year="2009", you should declare $year=2009. You don't need the curly brackets for else too.I think the code shoul be something like: <?php $year = 2009; if($year == 1998) { echo "YES"; } else echo "NO"; ?> Link to comment Share on other sites More sharing options...
Ingolme Posted September 4, 2009 Share Posted September 4, 2009 The $year variable is declared as a string, you must declare it as an integer. Instead of $year="2009", you should declare $year=2009. You don't need the curly brackets for else too.I think the code shoul be something like:<?php $year = 2009; if($year == 1998) { echo "YES"; } else echo "NO"; ?> Since there are no mathematical operations being done with it, it's just fine for it to be a string.As for the curly brackets, I find it preferrable to always use them. If you're going to remove them from the else you might as well remove them from the if() statemet as well. Link to comment Share on other sites More sharing options...
driz Posted September 4, 2009 Author Share Posted September 4, 2009 Thanks for the help. This also works: <?php $year = "2009"; if($year == "2005") : ?> <p>YES</p> <?php else : ?> <p>NO</p> <?php endif; ?> What is the advantage of doing it like this? Is this the only way to jump out of the PHP to start writing HTML inside the conditionals. Link to comment Share on other sites More sharing options...
Ingolme Posted September 4, 2009 Share Posted September 4, 2009 Thanks for the help. This also works:<?php $year = "2009"; if($year == "2005") : ?> <p>YES</p> <?php else : ?> <p>NO</p> <?php endif; ?> What is the advantage of doing it like this? Is this the only way to jump out of the PHP to start writing HTML inside the conditionals. You can enter and exit PHP blocks at any time.What's outside of the PHP blocks is equivalent to having an echo or print statement.There aren't advantages or disadvantages, it just depends on how you like to structure your code.Ideally, it would be good to keep the PHP and the HTML separated from eachother altogether, but that can be difficult.If you want an idea of how it works, you can view the source code of PHPBB or other software. Link to comment Share on other sites More sharing options...
driz Posted September 4, 2009 Author Share Posted September 4, 2009 What does the colan do? I tried it without the : and it doesn't work, so they are obviously required. Link to comment Share on other sites More sharing options...
Ingolme Posted September 4, 2009 Share Posted September 4, 2009 What does the colan do? I tried it without the : and it doesn't work, so they are obviously required. It indicates the beginning of a code block that belongs to a statement. You could put it this way as well, and it's probably better:<?php$year = "2009";if($year == "2005") {?> <p>YES</p><?php } else { ?> <p>NO</p><?php } ?> Link to comment Share on other sites More sharing options...
Synook Posted September 5, 2009 Share Posted September 5, 2009 The colon notation is for BASIC people but I do find it looks cleaner when you plan to exit the PHP block inside a conditional. The Wordpress code uses it. Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.