Php-mysql Course

Switch is another type of conditional, best used in place of a long if-elseif-else conditional.
Switch statement selects one of many blocks of code to be executed, by comparing the value of a single variable with a succession of values.

  - Syntax:
switch ($variable) {
  case 'value1':
    // Do this
    break;
  case 'value2':
    // Run other code
    break;
  default:
    // if nothing else is true, do this
}
- The value of the $variable within the brackets is the item being tested.
- The switch conditional compares the value of $variable with the values for each case. When it finds a match, the block of code associated with that case is executed, up until the break. If no match is found, the default statement is executed, assuming it exists (it's optional).
The switch conditional can only check a variable's value for equality.

The break statements are optional, but if you don't use them, PHP will continue to evaluate the next case and may execute some code further down the case tree.
The default statement is used if no match is found.


- Example:
<?php
$num = 2;

switch($num) {
  case 1:
    echo 'Small number';
    break;
  case 2:
    echo 'Medium number';
    break;
  case 3:
    echo 'Bigger number';
    break;
  default:
    echo 'No number between 1 and 3';
}
?>
This code will output: "Medium number".

• Another example, with a string as the value to be compared.
<?php
switch($today) {
  case 'Monday':
    echo 'Today is Monday, I go to school.';
    break;
  case 'Tuesday':
    echo 'Tuesday, lot of work to do.';
    break;
  case 'Sunday':
    echo 'Sunday, good day for rest.';
    break;
  default:
    echo 'A normal day';
}
?>

Daily Test with Code Example

HTML
CSS
JavaScript
PHP-MySQL
Which meta tag provides a short description of the page?
<meta content="..."> <meta description="..."> <meta http-equiv="...">
<meta name="description" content="70-160 characters that describes the content of the page" />
Which CSS property is used to stop the wrapping effect of the "float"?
clear text-align position
#some_id {
  clear: both;
}
Click on the method which gets an array with all the elements in the document that have a specified tag name.
getElementsByName() getElementById() getElementsByTagName()
var divs = document.getElementsByTagName("div");
var nr_divs = divs.length;
alert(nr_divs);
Indicate the PHP function which returns the number of elements in array.
is_[) count() strlen()
$arr =[7, 8, "abc", 10);
$nri = count($arr);
echo $nri;        // 4
PHP Switch Case

Last accessed pages

  1. String Object (667)
  2. Register and show online users and visitors (39428)
  3. PhpSpreadsheet - Read, Write Excel and LibreOffice Calc files (26057)
  4. Courses Web: PHP-MySQL JavaScript Node.js Ajax HTML CSS (137889)
  5. Adding text with ActionScript 3 (5501)

Popular pages this month

  1. Courses Web: PHP-MySQL JavaScript Node.js Ajax HTML CSS (202)
  2. Read Excel file data in PHP - PhpExcelReader (66)
  3. The Mastery of Love (56)
  4. PHP Unzipper - Extract Zip, Rar Archives (51)
  5. Working with MySQL Database (34)
Chat
Chat or leave a message for the other users
Full screenInchide