Php-mysql Course

PHP has three primary conditional statements: if, else and else if (which can also be written elseif).
Conditional statements are used to perform different actions based on different conditions.

1) Every conditional begins with an "if()" clause. This conditional executes some code only if a specified condition is TRUE.

  - Syntax:
if (condition) { // code to be executed if condition is TRUE }
  - Example:
<?php
$nr = 8;
if (isset($nr)) {
  echo 'Nr. is: '. $nr;
}
?>
This code will output: "Nr. is: 8".

The PHP isset() function is very often used with the "if" clause. This function checks if a variable is set (with a value other than NULL), returns TRUE if the variable is defined, otherwise, FALSE.


2) if...else statement executes some code if a condition is TRUE and another code if the condition is FALSE.
  - Syntax:
if (condition) { // code to be executed if condition is TRUE }
else { // code to be executed if condition is FALSE }
  - Example:
<?php
if (isset($nr)) {
  echo 'Nr. is: '. $nr;
}
else {
  echo 'The $nr variable is not defined';
}
?>
Becouse the $nr variable isn't set, the "isset($nr)" condition returns FALSE, this makes to be executed the code from "else" statement, and will output: "The $nr variable is not defined".

3) An elseif clause allows you to add multiple conditions.
  - Syntax:
if (condition1) { // code to be executed if condition1 is TRUE }
elseif (condition2) { // code to be executed if condition1 is FALSE and condition2 is TRUE }
else { // code to be executed if all conditions are FALSE }
You can use as many "elseif" clauses as you want (after the "if()"). If a condition is TRUE, the code in the following curly braces ({}) will be executed. If not, PHP will continue on. If there is a second condition (after an elseif), that will be checked for truth. The process will continue until PHP hits an "else", which will be automatically executed at that point, or until the conditional statement terminates without an "else". It's important that the "else" always come last and be treated as the default action.
Only the first condition that equates to TRUE will be executed; all others will be ignored, even if they're also True. This means you need to build conditional statements in the order of priority that you want them to be evaluated.
  - Example:
<?php
$nr = NULL;
if (isset($nr)) {
  echo 'Nr. is: '. $nr;
}
elseif(is_null($nr)) {
  echo 'Nr is NULL';
}
else {
  echo 'The $nr variable is not defined';
}
?>
isset($nr) returns TRUE only if $nr is set and with a value other than NULL. So, in the above code this condition returns FALSE, and PHP checks the next condition (of the "elseif" clause).
Here, the "is_null($nr)" returns TRUE, and the script will output: "Nr is NULL".

The PHP is_null($var) function finds whether the given variable is NULL. Returns TRUE if $var is null, FALSE otherwise.

Using if, else and elseif with Comparative and Logical Operators

Conditions often depend on the comparison of two values, and are used with comparative and logical operators.
The fallowing table shows the comparative and logical operators that can be used when writing conditionals.

Operator Description Example
== is equal to 8==8 returns true
=== is identical to 0===false returns false
!= is not equal 7!=8 returns true
<> is not equal 7<>8 (true)
> is greater than 7>8 (false)
< is less than 7<8 (true)
>= is greater than or equal to 8>=8 (true)
<= is less than or equal to 7<=8 (true)
&& and $x=8; $y=10;
($x<5 && $y>7)   // false
|| or $x=8; $y=10;
($x<5 || $y>7)   // true
! not $x=8; $y=10;
!($x==$y)   // true
- the === operator (three '=') checks if two expresions are identical (if they have the same value and the same data type). For example (0==false) may return true, but (0===false) returns false.
- && returns True only if both $x and $y are true.
- || returns True if at least one of the expression $x and $y are true.
- !$x returns True if $x is false.

Examples with conditionals and operators.
  1) Checks if two values are equals (the values can be strings too) [will dysplay "Hy MarPlo"]
<?php
$name = 'MarPlo';
if($name=='MarPlo') {
  echo 'Hy '. $name;
}
else {
  echo 'Hy there.';
}
?>

  2) Uses the "&&" and "||" operators to check two expresions [will dysplay "x<11 or y>32"]
<?php
$x = 8;
$y = 32;
if ($x<11 && $y>32) {
  echo 'x<11 and y>32';
}
elseif ($x<11 || $y>32) {
  echo 'x<11 or y>32';
}
else {
  echo $x. ' - '. $y;
}
?>

  3) You can nest conditionals. The next example will dysplay "It is a number less than 100 but greater than 10".
<?php
$num = 88;
if ($num > 10) {
  if ($num > 100)
    echo "It is a number greater than 100";
  else
    echo "It is a number less than 100 but greater than 10";
}
else echo "This is a small number";
?>

Ternary operator (the ? : operator)

The conditional operator ? ... : , called the ternary operator because it takes three operands (a condition, a result for true, and a result for false), is another way to make decisions in PHP.
This operator forms an expression which replaces the if ... else statement..
  - Syntax:
(condition) ? result_for_TRUE : result_for_FALSE;
  - Example:
<?php
$age = 12;

// ternary statement
$agestr = ($age < 18) ? 'child' : 'adult';

echo $agestr;           // child
?>
- In the ternary statement, first there is a condition ($age < 18), then there is a question mark (?), then a true-result, a colon (:), and a false-result. If $age is less than 18, $agestr will be set to 'child', otherwise it will be set to 'adult'.
This code can be expressed with an if ... else statement like this:
<?php
$age = 12;
if ($age < 18) {
  $agestr = 'child';
}
else {
  $agestr = 'adult';
}
echo $agestr;
?>

Daily Test with Code Example

HTML
CSS
JavaScript
PHP-MySQL
Which tag is used to add lists into <ul> and <ol> elements?
<dt> <dd> <li>
<ul>
 <li>http://coursesweb.net/html/</li>
 <li>http://coursesweb.net/css/</li>
</ul>
Which value of the "display" property creates a block box for the content and ads a bullet marker?
block list-item inline-block
.some_class {
  display: list-item;
}
Which instruction converts a JavaScript object into a JSON string.
JSON.parse() JSON.stringify eval()
var obj = {
 "courses": ["php", "javascript", "ajax"]
};
var jsonstr = JSON.stringify(obj);
alert(jsonstr);    // {"courses":["php","javascript","ajax"]}
Indicate the PHP class used to work with HTML and XML content in PHP.
stdClass PDO DOMDocument
$strhtml = '<body><div id="dv1">CoursesWeb.net</div></body>';
$dochtml = new DOMDocument();
$dochtml->loadHTML($strhtml);
$elm = $dochtml->getElementById("dv1");
echo $elm->nodeValue;    // CoursesWeb.net
If Else conditionals, Comparative and Logical operators

Last accessed pages

  1. Courses Web: PHP-MySQL JavaScript Node.js Ajax HTML CSS (141749)
  2. Node.js Move and Copy file (28420)
  3. MouseEvent - Events for Mouse (2909)
  4. PHPMailer (2311)
  5. Uploading images to server with Ajax (6095)

Popular pages this month

  1. Courses Web: PHP-MySQL JavaScript Node.js Ajax HTML CSS (473)
  2. CSS cursor property - Custom Cursors (79)
  3. The Mastery of Love (70)
  4. PHP-MySQL free course, online tutorials PHP MySQL code (62)
  5. CSS3 2D transforms (46)