Home arrow PHP arrow Page 2 - JV’s Power Tips for PHP (2)
PHP

JV’s Power Tips for PHP (2)


In this article Justin explores day to day programming techniques that can help to make a PHP developers life just that little bit easier.

Author Info:
By: Justin Vincent
Rating: 3 stars3 stars3 stars3 stars3 stars / 2
February 02, 2003
TABLE OF CONTENTS:
  1. · JV’s Power Tips for PHP (2)
  2. · The Problem
  3. · Method 1
  4. · Method 2
  5. · Method 3
  6. · Method 4
  7. · Conclusion

print this article
SEARCH DEVARTICLES

TOOLS YOU CAN USE

advertisement
JV’s Power Tips for PHP (2) - The Problem
(Page 2 of 7 )

We all want to get things done and there are many ways to skin a cat. For example: Imagine you were writing a live chat script and you wanted to give the user the option of formatting each submission with any combination of Bold, Italic, Underlined and/or Strike Through. Here is an example HTML form for such a script:

<form method=post action=my_script.php>
<input type=text name=chat>
<input type=checkbox name=bold value=1>
<input type=checkbox name=italic value=1>
<input type=checkbox name=underline value=1>
<input type=checkbox name=strike value=1>
<input type=submit>
</form>


Since this form has the action of post we will be evaluating the following variables:

$_POST['chat']
$_POST['bold']
$_POST['italic']
$_POST['underline']
$_POST['strike']


At its most basic we can test if the user has selected bold by doing this:

<?php

  if ( $_POST['bold'] )
  {
    echo "The user selected bold";
  }

?>


Just in case you don't know why this works. It is because if the bold checkbox was selected, then the value of 1 will be sent to PHP (within the variable $_POST['bold'])., but if the bold checkbox was not selected then the variable $_POST['bold'] will not be sent to PHP at all.

Furthermore, in PHP you are able to put a single variable into an if statement. The if statement will then evaluate the contents of the variable. It will evaluate to false under the following conditions:

  1. The variable does not exist
  2. The variable exists with a value of nothing
  3. The variable exists with a value of 0
  4. The variable exists with a boolean value of false
  5. The variable exists with a value of NULL
It will evaluate to true under the following conditions:

  1. The variable exists and has ANY value except empty, 0, NULL or false.
You can also evaluate an assignment within an if statement. Like so:

<?php
  $i_am_false = false;

  if ( $make_me_false = $i_am_false )
  {
    echo "This will never be output!";
  }
  else
  {
    echo ‘Because $make_me_false was assigned the value of false';
  }
?>


You can also put an array or an object within an if statement.

<?php

  if ( $my_object )
  {
    echo "My object exists!!";
  }

  if ( $my_array )
  {
    echo "My array exists!!";
  }

?>

blog comments powered by Disqus
PHP ARTICLES

- Removing Singletons in PHP
- Singletons in PHP
- Implement Facebook Javascript SDK with PHP
- Making Usage Statistics in PHP
- Installing PHP under Windows: Further Config...
- File Version Management in PHP
- Statistical View of Data in a Clustered Bar ...
- Creating a Multi-File Upload Script in PHP
- Executing Microsoft SQL Server Stored Proced...
- Code 10x More Efficiently Using Data Access ...
- A Few Tips for Speeding Up PHP Code
- The Modular Web Page
- Quick E-Commerce with PHP and PayPal
- Regression Testing With JMeter
- Building an Iterator with PHP

Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 



© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 7 - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials