Get Paid to Blog About the Things You Love

PHP if statement alternatives

Did you know there are actually 3 different ways to use the if…else…elseif control in PHP? They each have their uses, and hopefully I can help decode them a little bit for those who aren’t familiar with them. Here is your standard if statement:

if ($islocal) {
    $user = "localhost";
} else {
    $user = $logged_in_user; 
} 
if ($isadmin) {
    $ip = "192.168.0.1";
} else {
    $ip = "localhost";
} 

Now, there is nothing wrong with that, but if you’re looking for something a bit cleaner, you could try it this way:

$user = ($islocal) ? "localhost" : $logged_in_user;
$ip = ($isadmin) ? "192.168.0.1" : "localhost";

Less code. Less mess. Less parsing time. Very nice. Now, what about dynamically generating html pages? I’ll use a short example of the standard way, but in practice they get quite a bit more complicated and tough to read. Here’s an example of a form the standard way:

<?php
if ($isloggedin) {
   echo "<form name = 'myform' action = 'submit.php' method = 'post'>";
   echo "<input name = 'firstname' value = '{$user['firstname']}'
         type = 'text' />";
   echo "<input name = 'lastname' value = '{$user['lastname']}'
         type = 'text' />"; 
   echo "</form>"; 
} else  {
   echo "<div class = 'errormessage'>You are not logged in.</div>"; 
   echo "<div class = 'infotext'><p>Please <a href = 'login.php'>login
         </a> or <a href = 'register.php'>register</a> before attempting
         to edit your profile</p></div>";  
}
?> 

Now like I said, this one isn’t so bad. but imagine if you needed to display much more website content, and had to do it that way. Also, this has the caveat of being very unfriendly with any sort of visual editor like Dreamweaver. Here’s a better way:

<?php if ($isloggedin) : ?>
    <form name = 'myform' action = 'submit.php' method = 'post'>
    <input name = 'firstname' value = '<?php echo $user['firstname'] ?>'
     type = 'text' />
    <input name = 'lastname' value = '<?php echo $user['lastname']} ?>'
     type = 'text' />
    </form>
<?php else: ?>
    <div class = 'errormessage'>You are not logged in.</div>
    <div class = 'infotext'><p>Please <a href = 'login.php'>login</a> or 
    <a href = 'register.php'>register</a> before attempting to edit your
     profile</p></div>
<?php endif; ?> 

Now, this doesn’t save a whole lot of code space, but it does make everything a bit more readable. It also allows you to see exactly what you’re working with in something like dreamweaver, and copy and paste large amounts of code without having to scroll down line by line and copy/paste/copy/paste… 

Hopefully these will help save some of you some time. Good luck, and happy coding!

Stumble it!

Post Info

This entry was posted on Sunday, September 7th, 2008 and is filed under development, php. It has 37 views

Tags:, , , , , , , ,

Comments Feed. | Leave Comment | Leave Trackback.



Previous Post: RNC drops over 100,000 Balloons »
Next Post: Entrecard reciprocal dropping speed tips »
Most viewed posts


Leave a Reply

Note: Any comments are permitted only because the site owner is letting you post, and any comments will be removed for any reason at the absolute discretion of the site owner.