PHP continue statement

The PHP continue statement is used to continue the loop. It continues the current flow of the program and skips the remaining code at the specified condition.

The continue statement is used within looping and switch control structure when you immediately jump to the next iteration.

The continue statement can be used with all types of loops such as – for, while, do-while, and foreach loop. The continue statement allows the user to skip the execution of the code for the specified condition.

Syntax

The syntax for the continue statement is given below:

  1. jump-statement;  
  2. continue;  

Flowchart:

PHP continue statement

PHP Continue Example with for loop

Example

In the following example, we will print only those values of i and j that are same and skip others.

<?php  
    //outer loop  
    for ($i =1; $i<=3; $i++) {  
        //inner loop  
        for ($j=1; $j<=3; $j++) {  
            if (!($i == $j) ) {  
                continue;       //skip when i and j does not have same values  
            }  
            echo $i.$j;  
            echo "</br>";  
        }  
    }  
?>  

Output:

11
22 
33

PHP continue Example in while loop

Example

In the following example, we will print the even numbers between 1 to 20.

<?php  
    //php program to demonstrate the use of continue statement  
  
    echo "Even numbers between 1 to 20: </br>";  
    $i = 1;  
    while ($i<=20) {  
        if ($i %2 == 1) {  
            $i++;  
            continue;   //here it will skip rest of statements  
        }  
        echo $i;  
        echo "</br>";  
        $i++;  
    }     
?>  

Output:

Even numbers between 1 to 20: 
2
4
6
8
10
12
14
16
18
20

PHP continue Example with array of string

Example

The following example prints the value of array elements except those for which the specified condition is true and continue statement is used.

<?php  
    $number = array ("One", "Two", "Three", "Stop", "Four");  
    foreach ($number as $element) {  
        if ($element == "Stop") {  
            continue;  
        }  
        echo "$element </br>";  
    }  
?>  

Output:

One 
Two 
Three
Four

PHP continue Example with optional argument

The continue statement accepts an optional numeric value, which is used accordingly. The numeric value describes how many nested structures it will exit.

Example

Look at the below example to understand it better:

<?php  
    //outer loop  
    for ($i =1; $i<=3; $i++) {  
        //inner loop  
        for ($j=1; $j<=3; $j++) {  
            if (($i == $j) ) {      //skip when i and j have same values  
                continue 1;     //exit only from inner for loop   
            }  
            echo $i.$j;  
            echo "</br>";  
        }  
    }     
?>  

Output:

12
13
21 
23
31
32 

PHP Switch

PHP switch statement is used to execute one statement from multiple conditions. It works like PHP if-else-if statement.

Syntax

switch(expression){      
case value1:      
 //code to be executed  
 break;  
case value2:      
 //code to be executed  
 break;  
......      
default:       
 code to be executed if all cases are not matched;    
}  

Important points to be noticed about switch case:

  1. The default is an optional statement. Even it is not important, that default must always be the last statement.
  2. There can be only one default in a switch statement. More than one default may lead to a Fatal error.
  3. Each case can have a break statement, which is used to terminate the sequence of statement.
  4. The break statement is optional to use in switch. If break is not used, all the statements will execute after finding matched case value.
  5. PHP allows you to use number, character, string, as well as functions in switch expression.
  6. Nesting of switch statements is allowed, but it makes the program more complex and less readable.
  7. You can use semicolon (;) instead of colon (:). It will not generate any error.

PHP Switch Example

<?php      
$num=20;      
switch($num){      
case 10:      
echo("number is equals to 10");      
break;      
case 20:      
echo("number is equal to 20");      
break;      
case 30:      
echo("number is equal to 30");      
break;      
default:      
echo("number is not equal to 10, 20 or 30");      
}     
?>  

Output:

number is equal to 20

PHP switch statement with character

Program to check Vowel and consonant

We will pass a character in switch expression to check whether it is vowel or constant. If the passed character is A, E, I, O, or U, it will be vowel otherwise consonant.

<?php      
    $ch = 'U';  
    switch ($ch)  
    {     
        case 'a':   
            echo "Given character is vowel";  
            break;  
        case 'e':   
            echo "Given character is vowel";  
            break;  
        case 'i':   
            echo "Given character is vowel";  
            break;  
        case 'o':   
            echo "Given character is vowel";  
            break;    
        case 'u':   
            echo "Given character is vowel";  
            break;  
        case 'A':   
            echo "Given character is vowel";  
            break;  
        case 'E':   
            echo "Given character is vowel";  
            break;  
        case 'I':   
            echo "Given character is vowel";  
            break;  
        case 'O':   
            echo "Given character is vowel";  
            break;  
        case 'U':   
            echo "Given character is vowel";  
            break;  
        default:   
            echo "Given character is consonant";  
            break;  
    }  
?>  

Output:

Given character is vowel

PHP switch statement with String

PHP allows to pass string in switch expression. Let’s see the below example of course duration by passing string in switch case statement.

<?php      
    $ch = "B.Tech";  
    switch ($ch)  
    {     
        case "BCA":   
            echo "BCA is 3 years course";  
            break;  
        case "Bsc":   
            echo "Bsc is 3 years course";  
            break;  
        case "B.Tech":   
            echo "B.Tech is 4 years course";  
            break;  
        case "B.Arch":   
            echo "B.Arch is 5 years course";  
            break;  
        default:   
            echo "Wrong Choice";  
            break;  
    }  
?>

Output:

B.Tech is 4 years course

PHP switch statement is fall-through

PHP switch statement is fall-through. It means it will execute all statements after getting the first match, if break statement is not found.

<?php      
    $ch = 'c';  
    switch ($ch)  
    {     
        case 'a':   
            echo "Choice a";  
            break;  
        case 'b':   
            echo "Choice b";  
            break;  
        case 'c':   
            echo "Choice c";      
            echo "</br>";  
        case 'd':   
            echo "Choice d";  
            echo "</br>";  
        default:   
            echo "case a, b, c, and d is not found";  
    }  
?>  

Output:

Choice c
Choice d
case a, b, c, and d is not found

PHP nested switch statement

Nested switch statement means switch statement inside another switch statement. Sometimes it leads to confusion.

<?php      
    $car = "Hyundai";                   
        $model = "Tucson";    
        switch( $car )    
        {    
            case "Honda":    
                switch( $model )     
                {    
                    case "Amaze":    
                           echo "Honda Amaze price is 5.93 - 9.79 Lakh.";   
                        break;    
                    case "City":    
                           echo "Honda City price is 9.91 - 14.31 Lakh.";    
                        break;     
                }    
                break;    
            case "Renault":    
                switch( $model )     
                {    
                    case "Duster":    
                        echo "Renault Duster price is 9.15 - 14.83 L.";  
                        break;    
                    case "Kwid":    
                           echo "Renault Kwid price is 3.15 - 5.44 L.";  
                        break;    
                }    
                break;    
            case "Hyundai":    
                switch( $model )     
                {    
                    case "Creta":    
                        echo "Hyundai Creta price is 11.42 - 18.73 L.";  
                        break;    
        case "Tucson":    
                           echo "Hyundai Tucson price is 22.39 - 32.07 L.";  
                        break;   
                    case "Xcent":    
                           echo "Hyundai Xcent price is 6.5 - 10.05 L.";  
                        break;    
                }    
                break;     
        }  
?> 

Output:

Hyundai Tucson price is 22.39 - 32.07 L.

PHP If Else

PHP if else statement is used to test condition. There are various ways to use if statement in PHP.

  • if
  • if-else
  • if-else-if
  • nested if

PHP If Statement

PHP if statement allows conditional execution of code. It is executed if condition is true.

If statement is used to executes the block of code exist inside the if statement only if the specified condition is true.

Syntax

if(condition){  
//code to be executed  
} 

Flowchart

php if statement flowchart

Example

<?php  
$num=12;  
if($num<100){  
echo "$num is less than 100";  
}  
?>  

Output:

12 is less than 100

PHP If-else Statement

PHP if-else statement is executed whether condition is true or false.

If-else statement is slightly different from if statement. It executes one block of code if the specified condition is true and another block of code if the condition is false.

Syntax

if(condition){  
//code to be executed if true  
}else{  
//code to be executed if false  
}  

Flowchart

php if-else statement flowchart

Example

<?php  
$num=12;  
if($num%2==0){  
echo "$num is even number";  
}else{  
echo "$num is odd number";  
}  
?>  

Output:

12 is even number

PHP If-else-if Statement

The PHP if-else-if is a special statement used to combine multiple if?.else statements. So, we can check multiple conditions using this statement.

Syntax

if (condition1){    
//code to be executed if condition1 is true    
} elseif (condition2){      
//code to be executed if condition2 is true    
} elseif (condition3){      
//code to be executed if condition3 is true    
....  
}  else{    
//code to be executed if all given conditions are false    
}  

Flowchart

php if-else statement flowchart

Example

<?php  
    $marks=69;      
    if ($marks<33){    
        echo "fail";    
    }    
    else if ($marks>=34 && $marks<50) {    
        echo "D grade";    
    }    
    else if ($marks>=50 && $marks<65) {    
       echo "C grade";   
    }    
    else if ($marks>=65 && $marks<80) {    
        echo "B grade";   
    }    
    else if ($marks>=80 && $marks<90) {    
        echo "A grade";    
    }  
    else if ($marks>=90 && $marks<100) {    
        echo "A+ grade";   
    }  
   else {    
        echo "Invalid input";    
    }    
?>

Output:

B Grade

PHP nested if Statement

The nested if statement contains the if block inside another if block. The inner if statement executes only when specified condition in outer if statement is true.

Syntax

   

if (condition) {    
//code to be executed if condition is true   
if (condition) {    
//code to be executed if condition is true    
}    
}

Flowchart

php if-else statement flowchart

Example

<?php  
               $age = 23;  
    $nationality = "Indian";  
    //applying conditions on nationality and age  
    if ($nationality == "Indian")  
    {  
        if ($age >= 18) {  
            echo "Eligible to give vote";  
        }  
        else {    
            echo "Not eligible to give vote";  
        }  
    }  
?>  

Output:

Eligible to give vote

PHP Switch Example

<?php  
              $a = 34; $b = 56; $c = 45;  
    if ($a < $b) {  
        if ($a < $c) {  
            echo "$a is smaller than $b and $c";  
        }  
    }  
?> 

Output:

34 is smaller than 56 and 45

PHP Echo and print

We frequently use the echo statement to display the output. There are two basic ways to get the output in PHP:

  • echo
  • print

echo and print are language constructs, and they never behave like a function. Therefore, there is no requirement for parentheses. However, both the statements can be used with or without parentheses. We can use these statements to output variables or strings.

Difference between echo and print

echo

  • echo is a statement, which is used to display the output.
  • echo can be used with or without parentheses.
  • echo does not return any value.
  • We can pass multiple strings separated by comma (,) in echo.
  • echo is faster than print statement.

print

  • print is also a statement, used as an alternative to echo at many times to display the output.
  • print can be used with or without parentheses.
  • print always returns an integer value, which is 1.
  • Using print, we cannot pass multiple arguments.
  • print is slower than echo statement.

You can see the difference between echo and print statements with the help of the following programs.

PHP echo is a language construct, not a function. Therefore, you don’t need to use parenthesis with it. But if you want to use more than one parameter, it is required to use parenthesis.

The syntax of PHP echo is given below:

  1. void echo ( string $arg1 [, string $… ] )  

PHP echo statement can be used to print the string, multi-line strings, escaping characters, variable, array, etc. Some important points that you must know about the echo statement are:

  • echo is a statement, which is used to display the output.
  • echo can be used with or without parentheses: echo(), and echo.
  • echo does not return any value.
  • We can pass multiple strings separated by a comma (,) in echo.
  • echo is faster than the print statement.

PHP echo: printing string

<?php  
echo "Hello by PHP echo";  
?>  

Output:

Hello by PHP echo

PHP echo: printing multi line string

<?php  
echo "Hello by PHP echo  
this is multi line  
text printed by   
PHP echo statement  
";  
?>  

Output:

Hello by PHP echo this is multi line text printed by PHP echo statement

PHP echo: printing escaping characters

<?php  
echo "Hello escape \"sequence\" characters";  
?>  

Output:

Hello escape "sequence" characters

PHP echo: printing variable value

<?php  
$msg="Hello JavaTpoint PHP";  
echo "Message is: $msg";    
?>  

Output:

Message is: Hello JavaTpoint PHP

PHP Print

Like PHP echo, PHP print is a language construct, so you don’t need to use parenthesis with the argument list. Print statement can be used with or without parentheses: print and print(). Unlike echo, it always returns 1.

The syntax of PHP print is given below:

  1. int print(string $arg)  

PHP print statement can be used to print the string, multi-line strings, escaping characters, variable, array, etc. Some important points that you must know about the echo statement are:

  • print is a statement, used as an alternative to echo at many times to display the output.
  • print can be used with or without parentheses.
  • print always returns an integer value, which is 1.
  • Using print, we cannot pass multiple arguments.
  • print is slower than the echo statement.

PHP print: printing string

<?php  
print "Hello by PHP print ";  
print ("Hello by PHP print()");  
?>  

Output:

Hello by PHP print Hello by PHP print()

PHP print: printing multi line string

<?php  
print "Hello by PHP print  
this is multi line  
text printed by   
PHP print statement  
";  
?>  

Output:

Hello by PHP print this is multi line text printed by PHP print statement

PHP print: printing escaping characters

<?php  
print "Hello escape \"sequence\" characters by PHP print";  
?>  

Output:

Hello escape "sequence" characters by PHP print

PHP print: printing variable value

<?php  
$msg="Hello print() in PHP";  
print "Message is: $msg";    
?>  

Output:

Message is: Hello print() in PHP

php code in xampp

Generally, a PHP file contains HTML tags and some PHP scripting code. It is very easy to create a simple PHP example. To do so, create a file and write HTML tags + PHP code and save this file with .php extension.

Note: PHP statements ends with semicolon (;).

All PHP code goes between the php tag. It starts with <?php and ends with ?>. The syntax of PHP tag is given below:

<?php   
//your code here  
?>  

Let’s see a simple PHP example where we are writing some text using PHP echo command.

<!DOCTYPE>  
<html>  
    <body>  
       <?php  
          echo "<h2>Hello First PHP</h2>";  
        ?>  
    </body>  
</html> 

Output:

Hello First PHP

How to run PHP programs in XAMPP

How to run PHP programs in XAMPP PHP is a popular backend programming language. PHP programs can be written on any editor, such as – Notepad, Notepad++, Dreamweaver, etc. These programs save with .php extension, i.e., filename.php inside the htdocs folder.

For example – p1.php.

As I’m using window, and my XAMPP server is installed in D drive. So, the path for the htdocs directory will be “D:\xampp\htdocs”.

PHP program runs on a web browser such as – Chrome, Internet Explorer, Firefox, etc. Below some steps are given to run the PHP programs.

Step 1: Create a simple PHP program like hello world.

  1. <?php      
  2.     echo “Hello World!”;  
  3. ?>  

Step 2: Save the file with hello.php name in the htdocs folder, which resides inside the xampp folder.

Note: PHP program must be saved in the htdocs folder, which resides inside the xampp folder, where you installed the XAMPP. Otherwise it will generate an error – Object not found.

Step 3: Run the XAMPP server and start the Apache and MySQL.

Step 4: Now, open the web browser and type localhost http://localhost/hello.php on your browser window.

Step 5: The output for the above hello.php program will be shown as the screenshot below:

run PHP code in XAMPP

Most of the time, PHP programs run as a web server module. However, PHP can also be run on CLI (Command Line Interface).

PHP Case Sensitivity

In PHP, keyword (e.g., echo, if, else, while), functions, user-defined functions, classes are not case-sensitive. However, all variable names are case-sensitive.

In the below example, you can see that all three echo statements are equal and valid:

 

<!DOCTYPE>  
<html>  
    <body>  
        <?php  
            echo "Hello world using echo </br>";  
            ECHO "Hello world using ECHO </br>";  
            EcHo "Hello world using EcHo </br>";  
        ?>  
    </body>  
</html> 

Output:

Hello world using echo
Hello world using ECHO
Hello world using EcHo

Look at the below example that the variable names are case sensitive. You can see the example below that only the second statement will display the value of the $color variable. Because it treats $color, $ColoR, and $COLOR as three different variables:

<html>  
    <body>  
        <?php  
            $color = "black";  
            echo "My car is ". $ColoR ."</br>";  
            echo "My dog is ". $color ."</br>";  
            echo "My Phone is ". $COLOR ."</br>";  
        ?>  
    </body>  
</html>  

Output:

Notice: Undefined variable: ColoR in D:\xampp\htdocs\program\p2.php on line 8
My car is
My dog is black

Notice: Undefined variable: COLOR in D:\xampp\htdocs\program\p2.php on line 10
My Phone is

Only $color variable has printed its value, and other variables $ColoR and $COLOR are declared as undefined variables. An error has occurred in line 5 and line 7.

JavaScript Syntax

JavaScript has its own syntax and programming style. Syntax of a language defines rules of writing the code in that language, what is the correct way and what is not. In this tutorial, we will learn about the basic syntax for writing code in JavaScript.

JavaScript uses its own syntax to create variable, function, statements, etc. but if you have knowledge of any programming language like C, C++ or Java, it won’t be very difficult for you to understand the syntax of JavaScript.

1. A semicolon at the end of every statement (is Optional)

Semicolons are used to terminate the JavaScript code statements. A code statement is a line of code and we can mark the end of each code line by adding a semicolon. In languages like C, C++, Java, etc. we use a semicolon at the end of each code statement.

In JavaScript, statements are generally followed by a semicolon(;) which indicates termination of the statement.

However, it is not necessary to use a semicolon(;) at the end of the statement.

The following example shows two valid declarations of a JavaScript statement:

var var_name = value;

var var_name = value

In this example, the first statement is terminated using a semicolon, while the second statement is terminated without any semicolon. Both statements are valid in Javascript.

2. JavaScript White Spaces and Line Breaks

JavaScript interpreter ignores the tabs, spaces, and newlines that appear in the script, except for strings and regular expressions.

Below we have a simple code example where we have added an extra line break after each code statement and then the code without any space at all. Both are acceptable in JavaScript.

var i = 10;

var j = 20;

var a = i+j; var b = i-j;

You can write a more clean and readable JavaScript code by using multiple spaces as JavaScript ignores multiple spaces.

3. JavaScript Case Sensitivity

JavaScript is a case sensitive language, which means all the keywords, function names, variable names or identifiers should be typed with the consistent casing of letters. In general, a literal should start with a small letter and to combine the long name, we can use the camel case style too.

Let’s take an example, in the code below, all the three variables are distinct from each other because the first one is in the lower case, the second one starts with a capital letter, and the third one is in the upper case.

var marks;

var Marks;

var MARKS;

In camel-case style, all alphabets are in small expect for the starting alphabet of words, and the first alphabet will always be small. So if I want to name a variable or a function as myfirstvariable, the camel case style would be myFirstVariable.

4. JavaScript Comments

Comments refer to the text or code in a program that is ignored at the time of executing the program. Comments are used to provide additional information in the code, such as the description of code. And it is considered as good practice to add comments to your code.

Similar to other programming languages like C, C++, etc., JavaScript also defines two types of comments.

  • Single line Comments
  • Multiline Comments

Let’s take an example and understand how we can add these in our JavaScript code.

<html>
<head>
    <title>JavaScript Comment Example</title>
    <script>
    // This is an single line comment and it will not be executed
    /*
        This is a multiline comment and everything written 
        inside this block will not be executed. 
    */
    var x = 10;
    var y = 2*x;
    </script>
</head>
<body>
    <!-- Some HTML code - This is HTML comment -->
</body>
</html>

5. JavaScript Statements

Statements are known as the instructions in any programing language, these statements are set to be executed by the computer as a computer program.

JavaScript code is nothing but a list of these programming statements, these statements are collections of values, operators, expressions, keywords, and comments.

<html>
<head>
    <title>JavaScript Statement</title>
</head>
<body>
        <script>
         document.write("this is a text") ;   // JavaScript statement
         var a = 10+20;    // Another Statement
         document.write(a);
        </script>
</body>
</html>

In this tutorial, we explained the basic Javascript syntax and its usage with the help of examples. This will help you in getting a basic idea about writing the JavaScript code. We will be learning more about different JavaScript features like defining a variable, using operators, defining functions in upcoming tutorials and will cover the syntax for them in the respective tutorials.