Jump to content

Long script into PHP function


SE_Danny

Recommended Posts

Hi guys !So basicely, I have some variables $site,$title,$thumb. Now depending on the variable $site (if empty or not), I want to dislplay something different. So i thought the only way to do so is with some if else statements.But as I have to check the variables $site1 to $site9, it gives a long list of code, and I thing it will slow down the loading of the page.The idea is, when a user has not set up/choosen a value for $site, I want to show just something else than when he has already saved it.I'm having all this values stored on my database.But how can I do this without slowing down the hole thing ? A PHP function ? Another code ?example : checking for variable $site1

<?phpif($site1==""){$div1 = "<img class='over' src='/design/bigcube1.jpg' id='linksource1' onclick='showsource(1)' border='1' style='border-color:#000000;' height='100' width='100' />";}else{$div1 = "<div id='title1'>$title1</div><div id='edit1'><img class='over' src='/design/button-plus3.png' id='linksource1' onclick='showsource(1)'/></div><a target='_blank' href='$site1'><img id='thumb' src='$thumb1' border='1'/></a>";}?>

Thanks !! :)

Link to comment
Share on other sites

Hey Danny!What you are looking for is a switch statement. It looks like this:

switch($site){  case "site1":  // Code to handle if $site is equal to site1.  break;  case "site2":  // Code to handle if $site is equal to site2.  break;  case "site3":  // Code to handle if $site is equal to site3.  break;  case "site4":  // Code to handle if $site is equal to site4.  break;  default:  // Code to handle when $site is not equal to any of the above.  break;}

The switch statement checks if $site is equal to any of the cases below. If it equals one of the cases it runs the code for that particular case and then exits the statement when it hits "break;".

Link to comment
Share on other sites

Hey Danny!The switch statement checks if $site is equal to any of the cases below. If it equals one of the cases it runs the code for that particular case and then exits the statement when it hits "break;".
Thanks a lot ! The 'switch' does not actually make it faster, as it is basically the same thing as if .. else, but I got an awesome idea thanks to you :)
for ($i = 1; $i <= 9; $i++) {$test = "site$i";switch($$test){  case "":  echo "The $test is empty<br/>";  break;}}

So thanks !

Link to comment
Share on other sites

It won't really make a performance impact, but a switch is best suited for multiple options. An if statement would probably be better for your situation:

for ($i = 1; $i <= 9; $i++) {  $test = "site$i";  if ($$test == '')	echo "The $test is empty<br/>";}

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...