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 in <table> to create table header cell?
<thead> <th> <td>
<table><tr>
  <th>Title 1</th>
  <th>Title 2</th>
</tr></table>
Which CSS property sets the distance between lines?
line-height word-spacing margin
.some_class {
  line-height: 150%;
}
Which function opens a new browser window.
alert() confirm() open()
document.getElementById("id_button").onclick = function(){
  window.open("http://coursesweb.net/");
}
Indicate the PHP function that returns an array with names of the files and folders inside a directory.
mkdir() scandir() readdir()
$ar_dir = scandir("dir_name");
var_export($ar_dir);
If Else conditionals, Comparative and Logical operators

Last accessed pages

  1. Insert, Select and Update NULL value in MySQL (59194)
  2. Line and Text (1347)
  3. Validate radio and checkbox buttons (10078)
  4. CSS Content (425)
  5. Animated Tooltip with HTML and CSS (284)

Popular pages this month

  1. Courses Web: PHP-MySQL JavaScript Node.js Ajax HTML CSS (543)
  2. The Mastery of Love (65)
  3. CSS cursor property - Custom Cursors (63)
  4. Read Excel file data in PHP - PhpExcelReader (59)
  5. PHP-MySQL free course, online tutorials PHP MySQL code (44)