php unit 1
php unit 1
A variable is a data name that may be used to store a data vale. In PHP, a
variable is declared using a $ sign followed by the variable name. Here, some
important points to know about variables:
Let's see the example to store string, integer, and float values in PHP variables.
variable1.php
<?php
$str="hello string";
$x=200;
$y=44.6;
echo "string is: $str <br>";
echo "integer is: $x <br>";
echo "float is: $y <br>";
?>
Output:
variable2.php
<?php
$x=5;
$y=6;
$z=$x+$y;
echo $z;
?>
Output:
11
In PHP, variable names are case sensitive. So variable name "color" is different
from Color, COLOR, COLor etc.
variable3.php
<?php
$color="red";
echo "My car is " . $color . "<br>";
echo "My house is " . $COLOR . "<br>";
echo "My boat is " . $coLOR . "<br>";
?>
Output:
My car is red
Notice: Undefined variable: COLOR in C:\wamp\www\variable.php on line 4
My house is
Notice: Undefined variable: coLOR in C:\wamp\www\variable.php on line 5
My boat is
variablevalid.php
<?php
$a="hello";//letter (valid)
$_b="hello";//underscore (valid)
Output:
hello
hello
Variable Scope
The scope of a variable is defined as its range in the program under which it
can be accessed. In other words, "The scope of a variable is the portion of the
program within which it is defined and can be accessed."
PHP has three types of variable scopes:
Local variable
Global variable
Static variable
Local variable
The variables that are declared within a function are called local variables for
that function. These local variables have their scope only in that particular
function in which they are declared. This means that these variables cannot be
accessed outside the function, as they have local scope.
A variable declaration outside the function with the same name is completely
different from the variable declared inside the function.
local_variable1.php
<?php
function local_var()
{
$num = 45; //local variable
echo "Local variable declared inside the function is: ". $num;
}
local_var();
?>
Output:
Local variable declared inside the function is: 45
local_variable2.php
<?php
function mytest()
{
$lang = "PHP";
echo "Web development language: " .$lang;
}
mytest();
//using $lang (local variable) outside the function will generate an error
echo $lang;
?>
Output:
Web development language: PHP
Notice: Undefined variable: lang in D:\xampp\htdocs\program\p3.php on line
28
Global variable
The global variables are the variables that are declared outside the function.
These variables can be accessed anywhere in the program. There is no need to
use any keyword to access a global variable outside the function. To access the
global variable within a function, use the GLOBAL keyword before the variable.
Example:
global_variable1.php
<?php
$name = "Sanaya Sharma"; //Global Variable
function global_var()
{
global $name;
echo "Variable inside the function: ". $name;
echo "<br>";
}
global_var();
echo "Variable outside the function: ". $name;
?>
Output:
Variable inside the function: Sanaya Sharma
Variable outside the function: Sanaya Sharma
Note: Without using the global keyword, if you try to access a global variable
inside the function, it will generate an error that the variable is undefined.
Example:
global_variable2.php
<?php
$name = "Sanaya Sharma"; //global variable
function global_var()
{
echo "Variable inside the function: ". $name;
echo "<br>";
}
global_var();
?>
Output:
Notice: Undefined variable: name in D:\xampp\htdocs\program\p3.php on line
6
Variable inside the function:
Using $GLOBALS instead of global
Another way to use the global variable inside the function is predefined
$GLOBALS array.
Example:
global_variable3.php
<?php
$num1 = 5; //global variable
$num2 = 13; //global variable
function global_var()
{
$sum = $GLOBALS['num1'] + $GLOBALS['num2'];
echo "Sum of global variables is: " .$sum;
}
global_var();
?>
Output:
Sum of global variables is: 18
If two variables, local and global, have the same name, then the local variable
has higher priority than the global variable inside the function.
Example:
global_variable2.php
<?php
$x = 5;
function mytest()
{
$x = 7;
echo "value of x: " .$x;
}
mytest();
?>
Output:
Value of x: 7
Static variable
Constants
PHP constants are name or identifier that can't be changed during the
execution of the script. They remain constant across the entire program. PHP
constants follow the same PHP variable rules. For example, it can be started
with a letter or underscore only.
Conventionally, PHP constants should be defined in uppercase letters. Unlike
variables, constants are automatically global throughout the script. PHP
constants can be defined by 2 ways:
Using define() function
Using const keyword
define()
The define() function is used to create a constant. It defines constant at run
time. Let's see the syntax of define() function in PHP.
define(name, value, case-insensitive);
constant1.php
<?php
define("MESSAGE","Hello JavaTpoint PHP");
echo MESSAGE;
?>
Output:
constant2.php
<?php
define("MESSAGE","Hello JavaTpoint PHP",true);//not case sensitive
echo MESSAGE, "<br>";
echo message;
?>
Output:
Hello JavaTpoint PHP
Hello JavaTpoint PHP
constant3.php
<?php
define("MESSAGE","Hello JavaTpoint PHP",false);//case sensitive
echo MESSAGE;
echo message;
?>
Output:
const keyword
File: constant4.php
<?php
const MESSAGE="Hello const by JavaTpoint PHP";
echo MESSAGE;
?>
Output:
There is another way to print the value of constants using constant() function
instead of using the echo statement.
Syntax
constant (name)
File: constant5.php
<?php
define("MSG", "JavaTpoint");
echo MSG, "<br>";
echo constant("MSG");
//both are similar
?>
Output:
JavaTpoint
JavaTpoint
Constant vs Variables
Constant Variables
Once the constant is defined, it can never be A variable can be undefined as well as
redefined. redefined easily.
A constant can only be defined using define() A variable can be defined by simple
function. It cannot be defined by any simple assignment (=) operator.
assignment.
There is no need to use the dollar ($) sign To declare a variable, always use the
before constant during the assignment. dollar ($) sign before the variable.
Constants do not follow any variable scoping Variables can be declared anywhere in
rules, and they can be defined and accessed the program, but they follow variable
anywhere. scoping rules.
Constants are the variables whose values The value of the variable can be
can't be changed throughout the program. changed.
Data Types
Data types are used to hold different types of data or values. PHP supports 8
primitive data types that can be categorized further in 3 types:
1. Scalar Types (predefined)
2. Compound Types (user-defined)
3. Special Types
Scalar Types
It holds only single value. There are 4 scalar data types in PHP.
integer
float
string
boolean
Integer
Integer means numeric data with a negative or positive sign. It holds only
whole numbers, i.e., numbers without fractional part or decimal point.
Example:
<?php
$dec1 = 34;
$oct1 = 0243;
$hexa1 = 0x45;
echo "Decimal number: " .$dec1. "<br>";
echo "Octal number: " .$oct1. "<br>";
echo "HexaDecimal number: " .$hexa1. "<br>";
?>
Output:
Decimal number: 34
Octal number: 163
HexaDecimal number: 69
Float
Example:
<?php
$n1 = 19.34;
$n2 = 54.472;
$sum = $n1 + $n2;
echo "Addition of floating numbers: " .$sum;
?>
Output:
String
Example:
<?php
$company = "Javatpoint";
//both single and double quote statements will treat different
echo "Hello $company";
echo "<br>";
echo 'Hello $company';
?>
Output:
Hello Javatpoint
Hello $company
Boolean
Booleans are the simplest data type. It holds only two values: TRUE
(1) or FALSE (0). It is often used with conditional statements. If the condition is
correct, it returns TRUE otherwise returns FALSE.
Example:
<?php
if (TRUE)
echo "This condition is TRUE.";
if (FALSE)
echo "This condition is FALSE.";
?>
Output:
Compound Types
It can hold multiple values. There are 2 compound data types in PHP.
array
object
Array
An array is a compound data type. It can store multiple values of same data
type in a single variable.
Example:
<?php
$bikes = array ("Royal Enfield", "Yamaha", "KTM");
var_dump($bikes);
//the var_dump() function returns the datatype and values
echo "<br>";
echo "Array Element1: $bikes[0] <br>";
echo "Array Element2: $bikes[1] <br>";
echo "Array Element3: $bikes[2] <br>";
?>
Output:
Object
Objects are the instances of user-defined classes that can store both values
and functions. They must be explicitly declared.
Example:
<?php
class bike {
function model() {
$model_name = "Royal Enfield";
echo "Bike Model: " .$model_name;
}
}
$obj = new bike();
$obj -> model();
?>
Output:
resource
NULL
Resource
Resources are not the exact data type in PHP. Basically, these are used to store
some function calls or references to external PHP resources. For example - a
database call. It is an external resource.
Null
Null is a special data type that has only one value: NULL. There is a convention
of writing it in capital letters as it is case sensitive.
The special type of data type NULL defined a variable with no value.
Example:
<?php
$nl = NULL;
echo $nl; //it will not give any output
?>
Output:
Operators
Operator is a symbol that is used to perform operations on operands. In
simple words, operators are used to perform operations on variables or values.
PHP Operators can be categorized in following forms:
Arithmetic Operators
Assignment Operators
Bitwise Operators
Comparison Operators
Incrementing/Decrementing Operators
Logical Operators
String Operators
Array Operators
Type Operators
Execution Operators
Error Control Operators
We can also categorize operators on behalf of operands. They can be
categorized in 3 forms:
Arithmetic Operators
The arithmetic operators are used to perform common arithmetic operations such as
addition, subtraction, etc. with numeric values.
Assignment Operators
The assignment operators are used to assign value to different variables. The basic
assignment operator is "=".
Bitwise Operators
The bitwise operators are used to perform bit-level operations on operands. These
operators allow the evaluation and manipulation of specific bits within the integer.
& And $a & $b Bits that are 1 in both $a and $b are set to 1,
otherwise 0.
~ Not ~$a Bits that are 1 set to 0 and bits that are 0 are
set to 1
<< Shift left $a << $b Left shift the bits of operand $a $b steps
>> Shift right $a >> $b Right shift the bits of $a operand by $b number
of places
Comparison Operators
Comparison operators allow comparing two values, such as number or strings. Below the
list of comparison operators are given:
!== Not identical $a !== $b Return TRUE if $a is not equal to $b, and
they are not of same data type
Incrementing/Decrementing Operators
The increment and decrement operators are used to increase and decrease the value of a
variable.
Logical Operators
The logical operators are used to perform bit-level operations on operands. These
operators allow the evaluation and manipulation of specific bits within the integer.
xor Xor $a xor $b Return TRUE if either $a or $b is true but not both
String Operators
The string operators are used to perform the operation on strings. There are two string
operators in PHP are given below:
Array Operators
The array operators are used in array. Basically, these operators are used to compare the
values of arrays.
Type Operators
The type operator instanceof is used to determine whether an object, its parent and its
derived class are the same type or not. Basically, this operator determines which certain
class the object belongs to. It is used in object-oriented programming.
1. <?php
2. //class declaration
3. class Developer
4. {}
5. class Programmer
6. {}
7. //creating an object of type Developer
8. $charu = new Developer();
9.
10. //testing the type of object
11. if( $charu instanceof Developer)
12. {
13. echo "Charu is a developer.";
14. }
15. else
16. {
17. echo "Charu is a programmer.";
18. }
19. echo "<br>";
20. var_dump($charu instanceof Developer); //It will return true.
21. var_dump($charu instanceof Programmer); //It will return false.
22.?>
Output:
Charu is a developer.
bool(true) bool(false)
Execution Operators
PHP has an execution operator backticks (``). PHP executes the content of backticks as a
shell command. Execution operator and shell_exec() give the same result.
`` backtick echo Execute the shell command and return the result.
s `dir`; Here, it will show the directories available in current
folder.
PHP has one error control operator, i.e., at (@) symbol. Whenever it is used with an
expression, any error message will be ignored that might be generated by that expression.
[ array() left
** arithmetic right
| bitwise OR left
?: ternary left
or logical left
Arrays
What is an Array?
An array is a special variable that can hold many values under a single name,
and you can access the values by referring to an index number or name.
Array Types
Line breaks are not important, so an array declaration can span multiple lines:
$cars = [
"Volvo",
"BMW",
"Toyota"
];
$cars = [
"Volvo",
"BMW",
"Toyota",
];
Array Keys
When creating indexed arrays the keys are given automatically, starting at 0
and increased by 1 for each item, so the array above could also be created with
keys:
$cars = [
0 => "Volvo",
1 => "BMW",
2 =>"Toyota"
];
Indexed arrays are the same as associative arrays, but associative arrays have
names instead of numbers:
$myCar = [
];
You can declare an empty array first, and add items to it later:
$cars = [];
$cars[0] = "Volvo";
$cars[1] = "BMW";
$cars[2] = "Toyota";
The same goes for associative arrays, you can declare the array first, and then
add items to it:
$myCar = [];
$myCar["brand"] = "Ford";
$myCar["model"] = "Mustang";
$myCar["year"] = 1964;
You can have arrays with both indexed and named keys:
$myArr = [];
$myArr[0] = "apples";
$myArr[1] = "bananas";
$myArr["fruit"] = "cherries";
To access an array item, you can refer to the index number for indexed arrays,
and the key name for associative arrays.
echo $cars[2];
$cars = array("brand" => "Ford", "model" => "Mustang", "year" => 1964);
echo $cars["year"];
To update an existing array item, you can refer to the index number for
indexed arrays, and the key name for associative arrays.
$cars[1] = "Ford";
$cars = array("brand" => "Ford", "model" => "Mustang", "year" => 1964);
$cars["year"] = 2024;
To remove an existing item from an array, you can use the unset() function.
The unset() function deletes specified variables, and can therefore be used to
delete array items:
unset($cars[1]);
unset($cars[0], $cars[1]);
Indexed Arrays
By default, the first item has index 0, the second item has item 1, etc.
Create and display an indexed array:
var_dump($cars);
array(3) {
[0]=>
string(5) "Volvo"
[1]=>
string(3) "BMW"
[2]=>
string(6) "Toyota"
}
Access Indexed Arrays
echo $cars[0];
Volvo
Change Value
$cars[1] = "Ford";
var_dump($cars);
Associative Arrays
Associative arrays are arrays that use named keys that you assign to them.
$car = array("brand"=>"Ford", "model"=>"Mustang", "year"=>1964);
var_dump($car);
array(3) {
["brand"]=>
string(4) "Ford"
["model"]=>
string(7) "Mustang"
["year"]=>
int(1964)
}
echo $car["model"];
Mustang
Change Value
Example
$car["year"] = 2024;
var_dump($car);
array(3) {
["brand"]=>
string(4) "Ford"
["model"]=>
string(7) "Mustang"
["year"]=>
int(2024)
}
Multidimensional Arrays
PHP supports multidimensional arrays that are two, three, four, five, or more
levels deep. However, arrays more than three levels deep are hard to manage.
The dimension of an array indicates the number of indices you need to select
an element.
Two-dimensional Arrays
Volvo 22 18
BMW 15 13
Saab 5 2
Land Rover 17 15
We can store the data from the table above in a two-dimensional array, like
this:
$cars = array (
array("Volvo",22,18),
array("BMW",15,13),
array("Saab",5,2),
array("Land Rover",17,15)
);
Now the two-dimensional $cars array contains four arrays, and it has two
indices: row and column.
To get access to the elements of the $cars array we must point to the two
indices (row and column):
Sorting Arrays
The following example sorts the elements of the $cars array in ascending
alphabetical order:
sort($cars);
BMW
Toyota
Volvo
The following example sorts the elements of the $numbers array in ascending
numerical order:
$numbers = array(4, 6, 2, 22, 11);
sort($numbers);
2
4
6
11
22
The following example sorts the elements of the $cars array in descending
alphabetical order:
rsort($cars);
Volvo
Toyota
BMW
The following example sorts the elements of the $numbers array in descending
numerical order:
rsort($numbers);
asort($age);
Key=Peter, Value=35
Key=Ben, Value=37
Key=Joe, Value=43
ksort($age);
Key=Ben, Value=37
Key=Joe, Value=43
Key=Peter, Value=35
arsort($age);
Key=Joe, Value=43
Key=Ben, Value=37
Key=Peter, Value=35
Key=Peter, Value=35
Key=Joe, Value=43
Key=Ben, Value=37
Retrieving Array Size
An important task when using arrays, especially in combination with loops, is
finding out how many values the array contains. This is easily accomplished
with PHP’s count() function, which accepts the array variable as a parameter
and returns an integer value indicating how many elements it contains. Here’s
an example:
<?php
// define array
$data = array('Monday', 'Tuesday', 'Wednesday');
// get array size
echo 'The array has ' . count($data) . ' elements';
?>
3
Instead of the count() function, you can also use the sizeof() function, which
does the same thing.
Array Functions
PHP has a set of built-in functions that you can use on arrays. Function
array_column() Returns the values from a single column in the input array
array_combine() Creates an array by using the elements from one "keys" array a
"values" array
array_diff_assoc() Compare arrays, and returns the differences (compare keys and
array_diff_key() Compare arrays, and returns the differences (compare keys onl
array_diff_uassoc() Compare arrays, and returns the differences (compare keys and
using a user-defined key comparison function)
array_diff_ukey() Compare arrays, and returns the differences (compare keys onl
user-defined key comparison function)
array_intersect() Compare arrays, and returns the matches (compare values only
array_intersect_assoc() Compare arrays and returns the matches (compare keys and va
array_intersect_key() Compare arrays, and returns the matches (compare keys only)
array_intersect_uassoc() Compare arrays, and returns the matches (compare keys and v
a user-defined key comparison function)
array_intersect_ukey() Compare arrays, and returns the matches (compare keys only,
defined key comparison function)
array_replace() Replaces the values of the first array with the values from follo
array_replace_recursive() Replaces the values of the first array with the values from follo
recursively
array_search() Searches an array for a given value and returns the key
array_shift() Removes the first element from an array, and returns the value
removed element
array_udiff_assoc() Compare arrays, and returns the differences (compare keys and
using a built-in function to compare the keys and a user-define
to compare the values)
array_udiff_uassoc() Compare arrays, and returns the differences (compare keys and
using two user-defined key comparison functions)
array_uintersect() Compare arrays, and returns the matches (compare values only
user-defined key comparison function)
array_uintersect_assoc() Compare arrays, and returns the matches (compare keys and v
a built-in function to compare the keys and a user-defined func
compare the values)
array_uintersect_uassoc() Compare arrays, and returns the matches (compare keys and v
two user-defined key comparison functions)
each() Deprecated from PHP 7.2. Returns the current key and value pa
array
extract() Imports variables into the current symbol table from an array
Conditional Statements
if Statement
This statement executes the block of code inside the if statement if the
expression is evaluated as True.
Syntax
if (condition) {
Flowchart
Example:
<?php
$x = 12;
if ($x > 0) {
echo "The number is positive";
}
?>
Output:
The number is positive
if...else Statement
This statement executes the block of code inside the if statement if the
expression is evaluated as True and executes the block of code inside
the else statement if the expression is evaluated as False.
Syntax
if (condition) {
} else {
Flowchart
Example:
<?php
$x = -12;
if ($x > 0) {
echo "The number is positive";
}
else{
echo "The number is negative";
}
?>
Output:
The number is negative
If-else-if-else
This statement executes different expressions for more than two
conditions.
Syntax
if (condition) {
} elseif (condition) {
} else {
Flowchart:
Example:
<?php
$x = "August";
if ($x == "January") {
echo "Happy Republic Day";
}
else{
echo "Nothing to show";
}
?>
Output:
Happy Independence Day!!!
Nested if Statement
Example:
<?php
$a = 13;
Output:
Above 10
switch Statement
switch (expression) {
case label1:
//code block
break;
case label2:
//code block;
break;
case label3:
//code block
break;
default:
//code block
}
Flowchart:
Example:
<?php
$n = "February";
switch($n) {
case "January":
echo "Its January";
break;
case "February":
echo "Its February";
break;
case "March":
echo "Its March";
break;
case "April":
echo "Its April";
break;
case "May":
echo "Its May";
break;
case "June":
echo "Its June";
break;
case "July":
echo "Its July";
break;
case "August":
echo "Its August";
break;
case "September":
echo "Its September";
break;
case "October":
echo "Its October";
break;
case "November":
echo "Its November";
break;
case "December":
echo "Its December";
break;
default:
echo "Doesn't exist";
}
?>
Output:
Its February<?php
<?php
$i = "2";
switch ($i) {
case 0:
echo "i equals 0";
break;
case 1:
echo "i equals 1";
break;
case 2:
echo "i equals 2";
break;
default:
echo "i is not equal to 0, 1 or 2";
}
?>
Output:
i equals 2
Loops
PHP for loop can be used to traverse set of code for the specified number of
times.
It should be used if the number of iterations is known otherwise use while
loop. This means for loop is used when you already know how many times
you want to execute a block of code.
It allows users to put all the loop related statements in one place. See in the
syntax given below:
Syntax
initialization - Initialize the loop counter value. The initial value of the for
loop is done only once. This parameter is optional.
Flowchart
Example
1. <?php
2. for($n=1;$n<=10;$n++){
3. echo "$n<br/>";
4. }
5. ?>
Output:
5
6
10
Example
All three parameters are optional, but semicolon (;) is must to pass in for
loop. If we don't pass parameters, it will execute infinite.
1. <?php
2. $i = 1;
3. //infinite loop
4. for (;;) {
5. echo $i++;
6. echo "</br>";
7. }
8. ?>
Output:
We can use for loop inside for loop in PHP, it is known as nested for loop. The
inner for loop executes only when the outer for loop condition is found true.
In case of inner or nested for loop, nested for loop is executed fully for one
outer for loop. If outer for loop is to be executed for 3 times and inner for loop
for 3 times, inner for loop will be executed 9 times (3 times for 1st outer loop,
3 times for 2nd outer loop and 3 times for 3rd outer loop).
Example
1. <?php
2. for($i=1;$i<=3;$i++){
3. for($j=1;$j<=3;$j++){
4. echo "$i $j<br/>";
5. }
6. }
7. ?>
Output:
11
12
13
21
22
23
31
32
33
The foreach loop works on elements basis rather than index. It provides an easiest
way to iterate the elements of an array.
Syntax
1. foreach ($array as $value) {
2. //code to be executed
3. }
Syntax
1. foreach ($array as $key => $element) {
2. //code to be executed
3. }
Flowchart
Example 1:
PHP program to print array elements using foreach loop.
1. <?php
2. //declare array
3. $season = array ("Summer", "Winter", "Autumn", "Rainy");
4.
5. //access array elements using foreach loop
6. foreach ($season as $element) {
7. echo "$element";
8. echo "</br>";
9. }
10.?>
Output:
Summer
Winter
Autumn
Rainy
Example 2:
PHP program to print associative array elements using foreach loop.
1. <?php
2. //declare array
3. $employee = array (
4. "Name" => "Alex",
5. "Email" => "[email protected]",
6. "Age" => 21,
7. "Gender" => "Male"
8. );
9.
10. //display associative array element through foreach loop
11. foreach ($employee as $key => $element) {
12. echo $key . " : " . $element;
13. echo "</br>";
14. }
15. ?>
Output:
Name : Alex
Email : [email protected]
Age : 21
Gender : Male
Example 3:
Multi-dimensional array
1. <?php
2. //declare multi-dimensional array
3. $a = array();
4. $a[0][0] = "Alex";
5. $a[0][1] = "Bob";
6. $a[1][0] = "Camila";
7. $a[1][1] = "Denial";
8.
9. //display multi-dimensional array elements through foreach loop
10. foreach ($a as $e1) {
11. foreach ($e1 as $e2) {
12. echo "$e2\n";
13. }
14. }
15. ?>
Output:
Example 4:
Dynamic array
1. <?php
2. //dynamic array
3. foreach (array ('j', 'a', 'v', 'a', 't', 'p', 'o', 'i', 'n', 't') as $elements) {
4. echo "$elements\n";
5. }
6. ?>
Output:
j a v a t p o i n t
1
2
3
4
5
6
7
8
9
10
The while loop is also named as entry The do-while loop is also named as exit
control loop. control loop.
The body of the loop does not execute The body of the loop executes at least once,
if the condition is false. even if the condition is false.
Condition checks first, and then block Block of statements executes first and then
of statements executes. condition checks.
This loop does not use a semicolon to Do-while loop use semicolon to terminate the
terminate the loop. loop.
$i = 0;
$i = 0;
do
{
echo $users[$i]."\n";
$i++;
}
while($i < count($users));
?>
Output:
john
dave
tim
array using for loop in PHP
As shown below, it is exactly same as while loop, except it's condensed and
better syntax. Also, for one line statements, you can omit curly brackets ;)
<?php
$users = ['john', 'dave', 'tim'];
foreach($users as $user)
echo $user."\n";
?>
Output:
john
dave
tim
PHP break statement breaks the execution of the current for, while,
do-while, switch, and for-each loop. If you use break inside inner
loop, it breaks the execution of inner loop only. The break keyword
immediately ends the execution of the loop or switch structure.
PHP Break
PHP break statement breaks the execution of the current for, while, do-while,
switch, and for-each loop. If you use break inside inner loop, it breaks the
execution of inner loop only.
The break keyword immediately ends the execution of the loop or switch
structure. It breaks the current flow of the program at the specified condition
and program control resumes at the next statements outside the loop.
The break statement can be used in all types of loops such as while, do-while,
for, foreach loop, and also with switch case.
Syntax
1. jump statement;
2. break;
Flowchart