Quick php question

Associate
Joined
6 Jul 2003
Posts
2,075
Hi all,

I'm not a pro by any means but know the extreme basics of ASP VB. I know that IF statements can leak outside of the % tags, for basic example:

<% IF condition THEN %>

[Enter a load of html here]

<% ELSE %>

[More html here]

<% END IF %>


I'm trying to get to grips with the equivilent in php. Am I right in saying that every line of html produced as a result of an IF statement needs to have 'echo' in front of it, and stay within the php tag? Or can it be done similar to ASP?
 
In that case, to answer your question more fully:

You don't have to use echos - you can have anything in there. You can increment (add one to) a counter, assign a value to a variable, etc etc. It'll simply process everything within the statement if the arguement (if <condition>) is met.

Have a good read of that site (remember to scroll down for user examples of real world snippets of code) or get yourself a decent "Teach Yourself..." PHP book to work through :)
 
Commonly, if your wanting to print out the html within the <?php and ?> tags then you will need to do it using an echo statement such as:

Code:
<?php echo '<p>Hello</p>'; ?>

so for an if statement, it could be done like so:

Code:
<?php

$num = 1;

if ($num == 1) {
?>
<p>Num does equal 1
<?php
} else {
?>
Num does not equal 1
<?php
}

?>



So you can hope in and out of php throguhout your HTML. Just remember to use echo or something similar when outputting standard HTML within a php script.


I hope this is correct. Been doing PHP for a while but still learning.

Feel free to correct me if I am wrong.

Thanks

Regards,
Neil
 
You can do it all without jumping out of the php tags:

PHP:
<?php

$num = 1;

if ($num == 1) {
	$str = '<p>Num does equal 1';
} else {
	$str = 'Num does not equal 1';
}

echo $str;

?>
 
You can do it all without jumping out of the php tags:

PHP:
<?php

$num = 1;

if ($num == 1) {
    $str = '<p>Num does equal 1';
} else {
    $str = 'Num does not equal 1';
}

echo $str;

?>

If you're outputting a lot of stuff, that way is generally fastest (and most neat imo).
 
Back
Top Bottom