3rd-Bcac501 It PHP
3rd-Bcac501 It PHP
3rd-Bcac501 It PHP
PHP Introduction
What is PHP?
Before you continue you should have a basic understanding of the following:
• HTML
• CSS
• JavaScript
o PHP is a server scripting language, and a powerful tool for making dynamic
and interactive Web pages.
Characteristics of PHP
Five important characteristics make PHP's practical nature possible −
• Simplicity
• Efficiency
• PHP files can contain text, HTML, CSS, JavaScript, and PHP code
• PHP code is executed on the server, and the result is returned to the
browser as plain HTML
• PHP files have extension ".php"
With PHP you are not limited to output HTML. You can output images, PDF files,
and even Flash movies. You can also output any text, such as XHTML and XML.
Why PHP?
• PHP 7 is much faster than the previous popular stable release (PHP 5.6)
• PHP 7 has improved Error Handling
• PHP 7 supports stricter Type Declarations for function arguments
• PHP 7 supports new operators (like the spaceship operator: <=>)
What Do I Need?
PHP Features
PHP script is executed much faster than those scripts which are written in other
languages such as JSP and ASP. PHP uses its own memory, so the server workload
and loading time is automatically reduced, which results in faster processing speed
and better performance.
Open Source:
PHP source code and software are freely available on the web. You can develop all
the versions of PHP according to your requirement without paying any cost. All its
components are free to download and use.
PHP has easily understandable syntax. Programmers are comfortable coding with
it.
Embedded:
PHP code can be easily embedded within HTML tags and script.
Platform Independent:
PHP is available for WINDOWS, MAC, LINUX & UNIX operating system. A PHP
application developed in one OS can be easily executed in other OS also.
Database Support:
PHP supports all the leading databases such as MySQL, SQLite, ODBC, etc.
Error Reporting -
PHP allows us to use a variable without declaring its datatype. It will be taken
automatically at the time of execution based on the type of data it contains on its
value.
PHP is compatible with almost all local servers used today like Apache, Netscape,
Microsoft IIS, etc.
Control:
Different programming languages require long script or code, whereas PHP can do
the same work in a few lines of code. It has maximum control over the websites
like you can make changes easily whenever you want.
PHP Syntax
Basic PHP Syntax
<?php
// PHP code goes here
?>
A PHP file normally contains HTML tags, and some PHP scripting code.
Below, we have an example of a simple PHP file, with a PHP script that uses a
built-in PHP function "echo" to output the text "Hello World!" on a web page:
Example
<!DOCTYPE html>
<html>
<body>
<?php
echo "Hello World!";
?>
</body>
</html>
In PHP, keywords (e.g. if, else, while, echo, etc.), classes, functions, and user-
defined functions are not case-sensitive.
In the example below, all three echo statements below are equal and legal:
Example
<!DOCTYPE html>
<html>
<body>
<?php
ECHO "Hello World!<br>";
echo "Hello World!<br>";
EcHo "Hello World!<br>";
?>
</body>
</html>
Look at the example below; only the first statement will display the value of
the $color variable! This is because $color, $COLOR, and $coLOR are treated as
three different variables:
Example
<!DOCTYPE html>
<html>
<body>
<?php
$color = "red";
echo "My car is " . $color . "<br>";
echo "My house is " . $COLOR . "<br>";
echo "My boat is " . $coLOR . "<br>";
?>
</body>
</html>
A comment in PHP code is a line that is not executed as a part of the program. Its
only purpose is to be read by someone who is looking at the code.
Example
<?php
// This is a single-line comment
Example
<?php
/*
This is a multiple-lines comment block
that spans over multiple
lines
*/
?>
Example
<?php
// You can also use comments to leave out parts of a code line
$x = 5 /* + 15 */ + 5;
echo $x;
?>
In PHP, a variable starts with the $ sign, followed by the name of the variable:
Example
<!DOCTYPE html>
<html>
<body>
<?php
$txt = "Hello world!";
$x = 5;
$y = 10.5;
echo $txt;
echo "<br>";
echo $x;
echo "<br>";
echo $y;
?>
</body>
</html>
After the execution of the statements above, the variable $txt will hold the
value Hello world!, the variable $x will hold the value 5, and the variable $y will
hold the value 10.5.
Note: When you assign a text value to a variable, put quotes around the value.
Note: Unlike other programming languages, PHP has no command for declaring
a variable. It is created the moment you first assign a value to it.
A variable can have a short name (like x and y) or a more descriptive name (age,
carname, total_volume).
• A variable starts with the $ sign, followed by the name of the variable
• A variable name must start with a letter or the underscore character
• A variable name cannot start with a number
• A variable name can only contain alpha-numeric characters and
underscores (A-z, 0-9, and _ )
• Variable names are case-sensitive ($age and $AGE are two different
variables)
Output Variables
The PHP echo statement is often used to output data to the screen.
The following example will show how to output text and a variable:
Example
<!DOCTYPE html>
<html>
<body>
<?php
$txt = "TMSL";
echo "I love $txt!";
?>
</body>
</html>
The following example will produce the same output as the example above:
<!DOCTYPE html>
<html>
<body>
<?php
$txt = "TMSL";
echo "I love " . $txt . "!";
?>
</body>
</html>
Example
<?php
$x = 5;
$y = 4;
echo $x + $y;
?>
Note: You will learn more about the echo statement and how to output data to
the screen in the next chapter.
PHP automatically associates a data type to the variable, depending on its value.
Since the data types are not set in a strict sense, you can do things like adding a
string to an integer without causing an error.
In PHP 7, type declarations were added. This gives an option to specify the data
type expected when declaring a function, and by enabling the strict requirement,
it will throw a "Fatal Error" on a type mismatch.
The scope of a variable is the part of the script where the variable can be
referenced/used.
• local
• global
• static
A variable declared outside a function has a GLOBAL SCOPE and can only be
accessed outside a function:
Example
function myTest() {
// using x inside this function will generate an error
echo "<p>Variable x inside function is: $x</p>";
}
myTest();
A variable declared within a function has a LOCAL SCOPE and can only be
accessed within that function:
Example
<?php
function myTest() {
$x = 5; // local scope
echo "<p>Variable x inside function is: $x</p>";
}
myTest();
You can have local variables with the same name in different functions, because
local variables are only recognized by the function in which they are declared.
PROF. SANDIP GHOSHAL 12
PHP The global Keyword
The global keyword is used to access a global variable from within a function.
To do this, use the global keyword before the variables (inside the function):
Example
<?php
$x = 5;
$y = 10;
function myTest() {
global $x, $y;
$y = $x + $y;
}
myTest();
echo $y; // outputs 15
?>
Example
<?php
$x = 5;
$y = 10;
function myTest() {
$GLOBALS['y'] = $GLOBALS['x'] + $GLOBALS['y'];
}
myTest();
echo $y; // outputs 15
?>
Example
<?php
function myTest() {
static $x = 0;
echo $x;
$x++;
}
myTest();
myTest();
myTest();
?>
In this tutorial we use echo or print in almost every example. So, this chapter
contains a little more info about those two output statements.
The differences are small: echo has no return value while print has a return value
of 1 so it can be used in expressions. echo can take multiple parameters (although
such usage is rare) while print can take one argument. echo is marginally faster
than print.
The echo statement can be used with or without parentheses: echo or echo().
The following example shows how to output text with the echo command (notice
that the text can contain HTML markup):
Example
<?php
echo "<h2>PHP is Fun!</h2>";
echo "Hello world!<br>";
echo "I'm about to learn PHP!<br>";
echo "This ", "string ", "was ", "made ", "with multiple parameters.";
?>
Display Variables
The following example shows how to output text and variables with
the echo statement:
Example
<?php
$txt1 = "Learn PHP";
$txt2 = "TMSL";
$x = 5;
$y = 4;
The print statement can be used with or without parentheses: print or print().
Display Text
The following example shows how to output text with the print command (notice
that the text can contain HTML markup):
Example
<?php
print "<h2>PHP is Fun!</h2>";
print "Hello world!<br>";
print "I'm about to learn PHP!";
?>
Display Variables
The following example shows how to output text and variables with
the print statement:
Example
<?php
$txt1 = "Learn PHP";
$txt2 = "TMSL";
$x = 5;
$y = 4;
• String
• Integer
• Float (floating point numbers - also called double)
• Boolean
• Array
• Object
• NULL
• Resource
PHP String
A string can be any text inside quotes. You can use single or double quotes:
Example
<?php
$x = "Hello world!";
$y = 'Hello world!';
echo $x;
echo "<br>";
echo $y;
?>
PHP Integer
PHP Float
Example
<?php
$x = 10.365;
var_dump($x);
?>
PHP Boolean
$x = true;
$y = false;
Booleans are often used in conditional testing. You will learn more about
conditional testing in a later chapter of this tutorial.
In the following example $cars is an array. The PHP var_dump() function returns
the data type and value:
Example
<?php
$cars = array("Volvo","BMW","Toyota");
var_dump($cars);
?>
PHP Object
Classes and objects are the two main aspects of object-oriented programming.
When the individual objects are created, they inherit all the properties and
behaviors from the class, but each object will have different values for the
properties.
Let's assume we have a class named Car. A Car can have properties like model,
color, etc. We can define variables like $model, $color, and so on, to hold the
values of these properties.
When the individual objects (Volvo, BMW, Toyota, etc.) are created, they inherit
all the properties and behaviors from the class, but each object will have different
values for the properties.
If you create a __construct() function, PHP will automatically call this function
when you create an object from a class.
Null is a special data type which can have only one value: NULL.
A variable of data type NULL is a variable that has no value assigned to it.
Example
<?php
$x = "Hello world!";
$x = null;
var_dump($x);
?>
Example
Return the length of the string "Hello world!":
<?php
echo strlen("Hello world!"); // outputs 12
?>
Example
Count the number of word in the string "Hello world!":
<?php
echo str_word_count("Hello world!"); // outputs 2
?>
<?php
echo strrev("Hello world!"); // outputs !dlrow olleH
?>
Example
Search for the text "world" in the string "Hello world!":
<?php
echo strpos("Hello world!", "world"); // outputs 6
?>
Example
Replace the text "world" with "Dolly":
<?php
echo str_replace("world", "Dolly", "Hello world!"); // outputs Hello
Dolly!
?>
So, if you assign an integer value to a variable, the type of that variable will
automatically be an integer. Then, if you assign a string to the same
variable, the type will change to a string.
PHP Integers
2, 256, -256, 10358, -179567 are all integers.
Note: Another important thing to know is that even if 4 * 2.5 is 10, the
result is stored as float, because one of the operands is a float (2.5).
PHP has the following functions to check if the type of a variable is integer:
• is_int()
• is_integer() - alias of is_int()
• is_long() - alias of is_int()
<?php
$x = 5985;
var_dump(is_int($x));
$x = 59.85;
var_dump(is_int($x));
?>
PHP Floats
A float is a number with a decimal point or a number in exponential form.
PHP has the following predefined constants for floats (from PHP 7.2):
PHP has the following functions to check if the type of a variable is float:
• is_float()
• is_double() - alias of is_float()
Example
Check if the type of a variable is float:
<?php
$x = 10.365;
PHP Infinity
A numeric value that is larger than PHP_FLOAT_MAX is considered infinite.
• is_finite()
• is_infinite()
However, the PHP var_dump() function returns the data type and value:
Example
Check if a numeric value is finite or infinite:
<?php
$x = 1.9e411;
var_dump($x);
?>
PHP NaN
NaN stands for Not a Number.
• is_nan()
However, the PHP var_dump() function returns the data type and value:
Example
Invalid calculation will return a NaN value:
<?php
$x = acos(8);
var_dump($x);
?>
Example
Check if the variable is numeric:
<!DOCTYPE html>
<html>
<body>
<?php
// Check if the variable is numeric
$x = 5985;
var_dump(is_numeric($x));
echo "<br>";
$x = "5985";
var_dump(is_numeric($x));
echo "<br>";
$x = "59.85" + 100;
var_dump(is_numeric($x));
echo "<br>";
$x = "Hello";
var_dump(is_numeric($x));
?>
</body>
</html>
o/p
bool(true)
bool(true)
bool(true)
bool(false)
The (int), (integer), or intval() function are often used to convert a value to
an integer.
Example
Cast float and string to integer:
<?php
// Cast float to int
$x = 23465.768;
$int_cast = (int)$x;
echo $int_cast;
echo "<br>";
PHP Math
PHP pi() Function
The pi() function returns the value of PI:
Example
<?php
echo(min(0, 150, 30, 20, -8, -200)); // returns -200
echo(max(0, 150, 30, 20, -8, -200)); // returns 150
?>
Example
<?php
echo(abs(-6.7)); // returns 6.7
?>
Example
<?php
echo(sqrt(64)); // returns 8
?>
Example
<?php
echo(round(0.60)); // returns 1
echo(round(0.49)); // returns 0
?>
<!DOCTYPE html>
<html>
<body>
<?php
echo(round(0.60) . "<br>");
echo(round(0.50) . "<br>");
echo(round(0.49) . "<br>");
echo(round(-4.40) . "<br>");
echo(round(-4.60));
?>
</body>
</html>
O/P
1
1
0
-4
-5
Random Numbers
The rand() function generates a random number:
Example
<?php
echo(rand());
?>
O/P
578047231
PROF. SANDIP GHOSHAL 29
To get more control over the random number, you can add the
optional min and max parameters to specify the lowest integer and the
highest integer to be returned.
For example, if you want a random integer between 10 and 100 (inclusive),
use rand(10, 100):
Example
<?php
echo(rand(10, 100));
?>
O/P
97
PHP Constants
PHP Constants
A constant is an identifier (name) for a simple value. The value cannot be
changed during the script.
A valid constant name starts with a letter or underscore (no $ sign before the
constant name).
Note: Unlike variables, constants are automatically global across the entire
script.
Syntax
define(name, value, case-insensitive)
Parameters:
<?php
define("GREETING", "Welcome to TMSL!");
echo GREETING;
?>
Example
Create a constant with a case-insensitive name:
<?php
define("GREETING", "Welcome to TMSL", true);
echo greeting;
?>
Example
Create an Array constant:
<?php
define("cars", [
"Alfa Romeo",
"BMW",
"Toyota"
]);
echo cars[0];
?>
<?php
define("GREETING", "Welcome to W3Schools.com!");
function myTest() {
echo GREETING;
}
myTest();
?>
PHP Operators
PHP Operators
Operators are used to perform operations on variables and values.
• Arithmetic operators
• Assignment operators
• Comparison operators
• Increment/Decrement operators
• Logical operators
• String operators
• Array operators
• Conditional assignment operators
The basic assignment operator in PHP is "=". It means that the left operand
gets set to the value of the assignment expression on the right.
x += y x=x+ Addition
y
x -= y x=x- Subtraction
y
x *= y x=x* Multiplication
y
x /= y x=x/ Division
y
x %= y x=x% Modulus
y
Syntax
if (condition) {
code to be executed if condition is true;
}
Example
Output "Have a good day!" if the current time (HOUR) is less than 20:
<?php
$t = date("H");
Syntax
if (condition) {
code to be executed if condition is true;
} else {
Example
Output "Have a good day!" if the current time is less than 20, and "Have a
good night!" otherwise:
<?php
$t = date("H");
Syntax
if (condition) {
code to be executed if this condition is true;
} elseif (condition) {
code to be executed if first condition is false and this condition is
true;
} else {
code to be executed if all conditions are false;
}
Example
Output "Have a good morning!" if the current time is less than 10, and "Have
a good day!" if the current time is less than 20. Otherwise it will output
"Have a good night!":
<?php
$t = date("H");
Syntax
switch (n) {
case label1:
code to be executed if n=label1;
break;
case label2:
code to be executed if n=label2;
break;
case label3:
code to be executed if n=label3;
break;
...
default:
code to be executed if n is different from all labels;
}
switch ($favcolor) {
case "red":
echo "Your favorite color is red!";
break;
case "blue":
echo "Your favorite color is blue!";
break;
case "green":
echo "Your favorite color is green!";
break;
default:
echo "Your favorite color is neither red, blue, nor green!";
}
?>
PHP Loops
In the following chapters you will learn how to repeat code by using loops
in PHP.
PHP Loops
Often when you write code, you want the same block of code to run over and
over again a certain number of times. So, instead of adding several almost
equal code-lines in a script, we can use loops.
Loops are used to execute the same block of code again and again, as long as
a certain condition is true.
The following chapters will explain and give examples of each loop type.
PROF. SANDIP GHOSHAL 43
PHP while Loop
The while loop - Loops through a block of code as long as the specified
condition is true.
Syntax
while (condition is true) {
code to be executed;
}
Examples
The example below displays the numbers from 1 to 5:
Example
<?php
$x = 1;
while($x <= 5) {
echo "The number is: $x <br>";
$x++;
}
?>
Example Explained
• $x = 1; - Initialize the loop counter ($x), and set the start value to 1
• $x <= 5 - Continue the loop as long as $x is less than or equal to 5
• $x++; - Increase the loop counter value by 1 for each iteration
Example Explained
• $x = 0; - Initialize the loop counter ($x), and set the start value to 0
• $x <= 100 - Continue the loop as long as $x is less than or equal to
100
• $x+=10; - Increase the loop counter value by 10 for each iteration
Syntax
do {
code to be executed;
} while (condition is true);
Examples
The example below first sets a variable $x to 1 ($x = 1). Then, the do while
loop will write some output, and then increment the variable $x with 1. Then
Example
<?php
$x = 1;
do {
echo "The number is: $x <br>";
$x++;
} while ($x <= 5);
?>
This example sets the $x variable to 6, then it runs the loop, and then the
condition is checked:
Example
<?php
$x = 6;
do {
echo "The number is: $x <br>";
$x++;
} while ($x <= 5);
?>
Syntax
for (init counter; test counter; increment counter) {
code to be executed for each iteration;
}
Parameters:
Examples
The example below displays the numbers from 0 to 10:
<?php
for ($x = 0; $x <= 10; $x++) {
echo "The number is: $x <br>";
}
?>
Example Explained
• $x = 0; - Initialize the loop counter ($x), and set the start value to 0
• $x <= 10; - Continue the loop as long as $x is less than or equal to 10
• $x++ - Increase the loop counter value by 1 for each iteration
Example
<?php
for ($x = 0; $x <= 100; $x+=10) {
echo "The number is: $x <br>";
}
?>
Syntax
foreach ($array as $value) {
code to be executed;
}
For every loop iteration, the value of the current array element is assigned to
$value and the array pointer is moved by one, until it reaches the last array
element.
Examples
The following example will output the values of the given array ($colors):
<?php
$colors = array("red", "green", "blue", "yellow");
Example
<?php
$age = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43");
Example
<?php
for ($x = 0; $x < 10; $x++) {
if ($x == 4) {
break;
}
echo "The number is: $x <br>";
}
?>
PHP Continue
The continue statement breaks one iteration (in the loop), if a specified
condition occurs, and continues with the next iteration in the loop.
Break Example
<?php
$x = 0;
Continue Example
<?php
$x = 0;
PHP has more than 1000 built-in functions, and in addition you can create
your own custom functions.
Please check out our PHP reference for a complete overview of the PHP built-
in functions.
Syntax
function functionName() {
code to be executed;
}
Tip: Give the function a name that reflects what the function does!
Example
<?php
function writeMsg() {
echo "Hello world!";
}
Arguments are specified after the function name, inside the parentheses. You
can add as many arguments as you want, just separate them with a comma.
The following example has a function with one argument ($fname). When the
familyName() function is called, we also pass along a name (e.g. Jani), and
the name is used inside the function, which outputs several different first
names, but an equal last name:
Example
<?php
function familyName($fname) {
echo "$fname Refsnes.<br>";
}
familyName("Jani");
familyName("Hege");
familyName("Stale");
familyName("Kai Jim");
familyName("Borge");
?>
Example
<?php
function familyName($fname, $year) {
echo "$fname Refsnes. Born in $year <br>";
}
familyName("Hege", "1975");
familyName("Stale", "1978");
familyName("Kai Jim", "1983");
?>
In the following example we try to send both a number and a string to the
function without using strict:
Example
<?php
function addNumbers(int $a, int $b) {
return $a + $b;
}
echo addNumbers(5, "5 days");
// since strict is NOT enabled "5 days" is changed to int(5), and it
will return 10
?>
Example
<?php declare(strict_types=1); // strict requirement
Example
<?php declare(strict_types=1); // strict requirement
function setHeight(int $minheight = 50) {
echo "The height is : $minheight <br>";
}
setHeight(350);
setHeight(); // will use the default value of 50
setHeight(135);
setHeight(80);
?>
To declare a type for the function return, add a colon ( : ) and the type right
before the opening curly ( { )bracket when declaring the function.
In the following example we specify the return type for the function:
Example
<?php declare(strict_types=1); // strict requirement
function addNumbers(float $a, float $b) : float {
return $a + $b;
}
echo addNumbers(1.2, 5.2);
?>
You can specify a different return type, than the argument types, but make
sure the return is the correct type:
Example
<?php declare(strict_types=1); // strict requirement
function addNumbers(float $a, float $b) : int {
return (int)($a + $b);
}
echo addNumbers(1.2, 5.2);
?>
Example
Use a pass-by-reference argument to update a variable:
<?php
function add_five(&$value) {
$value += 5;
}
$num = 2;
add_five($num);
echo $num;
?>
PHP Arrays
An array stores multiple values in one single variable:
Example
<?php
$cars = array("Volvo", "BMW", "Toyota");
echo "I like " . $cars[0] . ", " . $cars[1] . " and " . $cars[2] . ".";
?>
What is an Array?
An array is a special variable, which can hold more than one value at a time.
If you have a list of items (a list of car names, for example), storing the cars
in single variables could look like this:
However, what if you want to loop through the cars and find a specific one?
And what if you had not 3 cars, but 300?
An array can hold many values under a single name, and you can access the
values by referring to an index number.
array();
Example
<?php
$cars = array("Volvo", "BMW", "Toyota");
echo count($cars);
?>
The index can be assigned automatically (index always starts at 0), like this:
$cars[0] = "Volvo";
$cars[1] = "BMW";
$cars[2] = "Toyota";
The following example creates an indexed array named $cars, assigns three
elements to it, and then prints a text containing the array values:
Example
<?php
$cars = array("Volvo", "BMW", "Toyota");
echo "I like " . $cars[0] . ", " . $cars[1] . " and " . $cars[2] . ".";
?>
Example
<?php
$cars = array("Volvo", "BMW", "Toyota");
$arrlength = count($cars);
for($x = 0; $x < $arrlength; $x++) {
echo $cars[$x];
echo "<br>";
}
?>
or:
$age['Peter'] = "35";
$age['Ben'] = "37";
$age['Joe'] = "43";
Example
<?php
$age = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43");
echo "Peter is " . $age['Peter'] . " years old.";
?>
Example
<?php
$age = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43");
However, sometimes you want to store values with more than one key.
For this, we have 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 for most people.
Volvo 22 18
BMW 15 13
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):
Example
<?php
<?php
$cars = array (
array("Volvo",22,18),
array("BMW",15,13),
array("Saab",5,2),
array("Land Rover",17,15)
);
</body>
</html>
Example
<!DOCTYPE html>
<html>
<body>
<?php
$cars = array("Volvo", "BMW", "Toyota");
sort($cars);
$clength = count($cars);
for($x = 0; $x < $clength; $x++) {
echo $cars[$x];
echo "<br>";
}
?>
</body> </html>
The following example sorts the elements of the $numbers array in
ascending numerical order:
Example
<!DOCTYPE html>
<html>
<body>
<?php
$numbers = array(4, 6, 2, 22, 11);
sort($numbers);
$arrlength = count($numbers);
for($x = 0; $x < $arrlength; $x++) {
echo $numbers[$x];
echo "<br>";
}
?>
</body>
Example
Example
<!DOCTYPE html>
<html>
<body>
<?php
$cars = array("Volvo", "BMW", "Toyota");
rsort($cars);
$clength = count($cars);
for($x = 0; $x < $clength; $x++) {
echo $cars[$x];
echo "<br>";
}
?>
</body>
</html>
EXAMPLE 2:-
<!DOCTYPE html>
<html>
<body>
<?php
$numbers = array(4, 6, 2, 22, 11);
rsort($numbers);
$arrlength = count($numbers);
for($x = 0; $x < $arrlength; $x++) {
echo $numbers[$x];
echo "<br>";
}
?>
Example
<!DOCTYPE html>
<html>
<body>
<?php
$age = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43");
asort($age);
</body>
</html>
O/P:-
Key=Peter, Value=35
Key=Ben, Value=37
Key=Joe, Value=43
<?php
$age = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43");
ksort($age);
</body>
</html>
Key=Ben, Value=37
Key=Joe, Value=43
Key=Peter, Value=35
Example
<!DOCTYPE html>
<html>
<body>
<?php
$age = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43");
arsort($age);
</body>
Key=Joe, Value=43
Key=Ben, Value=37
Key=Peter, Value=35
Example
<!DOCTYPE html>
<html>
<body>
<?php
$age = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43");
krsort($age);
</body>
</html>
Key=Peter, Value=35
Key=Joe, Value=43
Key=Ben, Value=37