switch statments

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
n8w
Forum Newbie
Posts: 5
Joined: Fri May 14, 2004 2:13 pm

switch statments

Post by n8w »

Hello .. I was able to do this in VB so .. I am sure I can do it in PHP .... anyway how do you write an if else statement or switch statment so you accomplish the following

Code: Select all

<? if statment is true?> 

tons of html code

<? else ?> 

tons more of different html code

<? endif?>
User avatar
CoderGoblin
DevNet Resident
Posts: 1425
Joined: Tue Mar 16, 2004 10:03 am
Location: Aachen, Germany

Post by CoderGoblin »

Code: Select all

if ($var == "xyz") &#123;
  // tons of code or call to function
  echo($htmlcode);
&#125; else &#123;
  // tons of different code or function
  echo ($htmlcode);
&#125;
Basic PHP. Switch is used for an if then elseif ... structure (CASE I believe in VB).

Regards
User avatar
patrikG
DevNet Master
Posts: 4235
Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK

Post by patrikG »

[php_man]switch[/php_man]
n8w
Forum Newbie
Posts: 5
Joined: Fri May 14, 2004 2:13 pm

one more question

Post by n8w »

thanks for replying

I tried something similar to what you wrote .. but I am getting a parsing error .. I think because I am trying to dynamically write other stuff as well

basically what I would like to do is

Code: Select all

<?php
$tempdog=$_GET&#1111;'id']; 
$length=strlen($tempdog);
if ( $length > 0 ) &#123;


==========================
write this code
==========================
<body onLoad="MM_preloadImages('images/<? echo $_GET&#1111;'id']; ?>-m.gif','images/<? echo $_GET&#1111;'id']; ?>-l.gif')">

 &#125; else &#123;
==========================
write this code
==========================
<body>
&#125;

?>
User avatar
launchcode
Forum Contributor
Posts: 401
Joined: Tue May 11, 2004 7:32 pm
Location: UK
Contact:

Post by launchcode »

You need to break out of your code, but essentially what you want to do will work fine - but before all the "=== write this code" stuff, break out of PHP first.
jason
Site Admin
Posts: 1767
Joined: Thu Apr 18, 2002 3:14 pm
Location: Montreal, CA
Contact:

Post by jason »

Code: Select all

<?php if ( $something_is_true ) : ?>
...lots of HTML....
<?php else: ?>
...lots of HTML....
<?php endif; ?>
I find using that method nice when mixing PHP with HTML. The endif; is clearer than a }, because with a } you don't know what it's being used for without some checking. The endif; makes it clear. So if you are mixing a few block level elements like a foreach, a while and an if, it makes reading easier.
Post Reply