PHP Shorthand Writing ” if else “

Here is how to write if else shorthand writing. It is useful in your code in order to keep your code neat and tidy. Assume you have a HTML form with input name “WEBSITE_TITLE” which will then be posted to a processing page.

$WEBSITE_TITLE_VAR = !empty($_POST['WEBSITE_TITLE']) ? $_POST['WEBSITE_TITLE'] : null;

 

How it works :

  1. Set your condition at beginning of the statement and the opposite value of the false condition.  Or you can also do it the other way round  which is true -> false OR false ->true.
  2. 1st it will check whether the value of $_POST['WEBSITE_TITLE'] is empty or not.
  3. If $_POST['WEBSITE_TITLE'] is  not empty, then it will hold the posted value
  4. Else “?“ $WEBSITE_TITLE_VAR will be null.

Leave a Reply

Your email address will not be published. Please enter your name, email and a comment.

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>