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 :
- 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.
- 1st it will check whether the value of $_POST['WEBSITE_TITLE'] is empty or not.
- If $_POST['WEBSITE_TITLE'] is not empty, then it will hold the posted value
- Else “?“ $WEBSITE_TITLE_VAR will be null.