Jump to content

Simple Php Help


driz
 Share

Recommended Posts

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

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. Edited by Ingolme
Link to comment
Share on other sites

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

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

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

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. Edited by Ingolme
Link to comment
Share on other sites

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

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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...