Advantages of PHP: Open Source Platform Independent Easy To Learn Database Support
Advantages of PHP: Open Source Platform Independent Easy To Learn Database Support
PHP
List advantages of PHP
Advantages of PHP
PHP (Hypertext Preprocessor) is widely used for web development due to its
many advantages:
Open Source – Free to use and has a large community for support.
a) strlen($string)
Returns the length of the string.
Example:
<?php
echo strlen("Hello World!"); // Output: 12
?>
b) strtoupper($string)
PHP 1
Converts a string to uppercase.
Example:
<?php
echo strtoupper("hello"); // Output: HELLO
?>
c) strtolower($string)
Converts a string to lowercase.
Example:
<?php
echo strtolower("HELLO"); // Output: hello
?>
Example:
<?php
echo str_replace("World", "PHP", "Hello World!"); // Output: Hello P
HP!
?>
1. String: "Hello"
2. Integer: 123
PHP 2
5. Array: ["apple", "banana", "cherry"]
array_flip()
Swaps keys and values in an array.
Syntax:
<?php
array_flip(array $array)
?>
Example:
<?php
$arr = ["a" => "apple", "b" => "banana"];
print_r(array_flip($arr));
// Output: ["apple" => "a", "banana" => "b"]
?>
explode()
Splits a string into an array based on a delimiter.
Syntax:
<?php
explode(string $separator, string $string, int $limit = PHP_INT_MAX)
>?
Example:
PHP 3
<?php
$str = "red,green,blue";
print_r(explode(",", $str));
// Output: ["red", "green", "blue"]
>?
for Loop
Used when the number of iterations is known.
Example:Output: 12345
<?php
for ($i = 1; $i <= 5; $i++) {
echo $i . " ";
}
?>
foreach Loop
Used for iterating over an array.
<?php
$colors = ["red", "green", "blue"];
foreach ($colors as $color) {
echo $color . " ";
}
?>
PHP 4
2. | (OR)
3. ^ (XOR)
4. ~ (NOT)
Example:
<?php
$a = 5; // 0101 in binary
$b = 3; // 0011 in binary
echo $a & $b; // Output: 1 (0001 in binary)
?>
<?php
$matrix = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
];
echo $matrix[1][2]; // Output: 6
?>
PHP 5
Multiple Supports multiple values ( echo "A",
Only one value allowed
Outputs "B"; )
Conclusion:
✔ Use echo for speed & multiple outputs.
a) Anonymous Function
An
anonymous function, also known as a lambda function, is a function without
a name. It is usually assigned to a variable and can be used as a callback
function or closure.
Use Cases:
Used for callback functions in array functions like array_map() , array_filter() ,
etc.
<?php
$greet = function($name) {
return "Hello, $name!";
};
PHP 6
echo $greet("John");
?>
b)
Variable Functions
Variable functions allow you to call a function using the value of a variable as
its name.
Use Cases:
Dynamic function calling: When function names are stored in variables.
<?php
function hello() {
return "Hello World!";
}
$func = "hello";
echo $func(); // Output: Hello World!
?>
Example:
$name = "PHP";
<?php
$i = 1;
do {
PHP 7
echo $i . " ";
$i++;
} while ($i <= 5);
?>
1) Associative Arrays
Uses custom string keys instead of numeric indexes.
Example use: Storing student marks ( "John" => 85, "Jane" => 90 ).
Example:
2) Multidimensional Arrays
Contains arrays inside arrays, used for structured data (tables, grids).
Example:
$students = [
["John", 25],
["Jane", 30]
];
PHP 8
Joins array elements into a
Purpose Splits a string into an array
string
Used to join elements (e.g., ", Used to split string into parts (e.g.,
Separator
") "," )
Example
"Apple, Banana, Cherry" ["Apple", "Banana", "Cherry"]
Output
Conclusion:
Use implode() when you need to convert an array into a string (like joining
words into a sentence).
Use explode() when you need to split a string into an array (like separating
words in a sentence).
Characteristics:
Defined using the function keyword.
Example:
<?php
function greet($name) {
return "Hello, $name!";
PHP 9
}
echo greet("John");
?>
1. Indexed Arrays
2. Associative Arrays
3. Multidimensional Arrays
<?php
function checkEvenOdd($num) {
return ($num % 2 == 0) ? "Even" : "Odd";
}
echo checkEvenOdd(4); // Output: Even
?>
<?php
function sumOfDigits($num) {
$sum = 0;
while ($num > 0) {
$sum += $num % 10;
$num = (int)($num / 10);
}
return $sum;
}
echo sumOfDigits(123); // Output: 6
?>
PHP 10
<?php
function factorial($num) {
return ($num == 1) ? 1 : $num * factorial($num - 1);
}
echo factorial(5); // Output: 120
?>
Key Points:
No Need to Declare Data Types: A variable can store different types of
values at different times.
Example:
<?php
$var = "10"; // integer
$var = "Helloo"; // now String
$sum = "10" + 5; // PHP converts "10" to integer, result = 15
echo"$var";
?>
PHP 11
Represents a normal Represents a variable variable (variable
Definition
variable name stored in another variable)
Example Explanation:
Conclusion:
$ is a normal variable storing a value.
PHP 12