updated on: 19-Dec-2022
PHP (recursive acronym for PHP: Hypertext Preprocessor) is a widely-used open source general-purpose scripting language that is especially suited for web development and can be embedded into HTML.
Further reading: - Intro. to 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.
XAMPP is the most popular PHP development environment. XAMPP is a completely free, easy to install Apache distribution containing MariaDB, PHP, and Perl. The XAMPP open source package has been set up to be incredibly easy to install and to use.
<!DOCTYPE html>
<html>
<head>
<title>Example</title>
</head>
<body>
<?php
echo "Hi, I'm a PHP script!";
?>
</body>
</html>
phpinfo — Outputs information about PHP’s configuration
<?php
// Show all information, defaults to INFO_ALL
phpinfo();
// Show just the module information.
// phpinfo(8) yields identical results.
phpinfo(INFO_MODULES);
?>
In PHP, all keywords (e.g. if, else, while, echo, etc.), classes, functions, and user-defined functions are NOT case-sensitive.
<?php
ECHO "Hello World!<br>";
echo "Hello World!<br>";
EcHo "Hello World!<br>";
?>
In PHP variable can be declared as: $var_name = value;
Further reading: https://www.tutorialrepublic.com/php-tutorial/php-variables.php
<?php
$color = "red";
$COLOR="GREEN";
echo "My car is " . $color . "<br>";
echo "My house is " . $COLOR . "<br>";
echo "My boat is " . $coLOR . "<br>";
?>
<?php
$txt = "W3Schools.com";
echo "I love $txt!";
// produce the same output
echo "<br>";
echo "I love " . $txt . "!";
?>
<?php
$x = 5;
$y = 4;
echo $x + $y;
?>
Further reading: https://www.php.net/manual/en/language.variables.scope.php
Local scope variable
<?php
function myTest() {
$x = 5; // local scope
echo "<p>Variable x inside function is: $x</p>";
}
myTest();
// using x outside the function will generate an error
echo "<p>Variable x outside function is: $x</p>";
?>
Global scope variable
<?php
$x = 5; // global scope
function myTest() {
// using x inside this function will generate an error
echo "<p>Variable x inside function is: $x</p>";
}
myTest();
echo "<p>Variable x outside function is: $x</p>";
?>
The global keyword
<?php
$x = 5; // global scope
$y = 10;
function myTest() {
global $x, $y; // use global scope variable in function
$y = $x + $y;
}
myTest();
echo $y; // outputs 15
?>
$GLOBALS - The $GLOBALS array is an associative array with the name of the global variable being the key and the contents of that variable being the value of the array element.
<?php
$x = 5;
$y = 10;
function myTest() {
$GLOBALS['y'] = $GLOBALS['x'] + $GLOBALS['y'];
}
myTest();
echo $y; // outputs 15
?>
<?php
function test()
{
$a = 0;
echo $a;
$a++;
}
?>
<?php
function myTest() {
static $x = 0;
echo $x . "<br \>";
$x++;
}
myTest();
myTest();
myTest();
myTest();
?>
strlen
<?php
echo "Length of a String: ";
echo strlen("Hello world!"); // outputs 12
?>
str_word_count
<?php
echo "<br /> Count Words: ";
echo str_word_count("Hello world!"); // outputs 2
?>
strrev
<?php
echo "<br />Reverse a String: ";
echo strrev("Hello world!"); // outputs !dlrow olleH
?>
strpos
<?php
echo "<br />Position: ";
echo strpos("Hello world!", "world"); // outputs 6
?>
str_replace
<?php
echo "<br />Replace: ";
echo str_replace("world", "PHP", "Hello world!"); // outputs Hello Dolly!
?>
<!DOCTYPE html>
<html>
<body>
<?php
// This is a single-line comment
# This is also a single-line comment
/*
This is a multiple-lines comment block
that spans over multiple
lines
*/
// You can also use comments to leave out parts of a code line
$x = 5 /* + 15 */ + 5;
$name = "Muhammad Ahmad Nasir";
echo "<h2>$name</h2>";
echo '<h1>' .$x. '</h1>';
?>
</body>
</html>
define — Defines a named constant
Further reading: - https://www.php.net/manual/en/function.define.php
<!DOCTYPE html>
<html lang="en">
<head>
<title>PHP - Contants</title>
</head>
<body>
<?php
// define(name, value, case-insensitive)
// case-insensitive: Specifies whether the constant name should be case-insensitive. Default is false
define("GREETING", "Welcome to Department of CS & IT!");
function myTest() {
echo GREETING;
}
myTest();
?>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<title>PHP - Functions</title>
</head>
<body>
<?php
// Syntax
//
/*function functionName() {
code to be executed;
}*/
//
// Example1
//
function writeMsg() {
echo "Hello world!";
}
writeMsg(); // call the function
//
// Example2 - By Argument
//
function familyName($fname) {
echo "$fname Nasir" . "<br>";
}
familyName("Muhammad"); // call
familyName("Ali");
familyName("Zeeshan");
// Example3 - By two arguments
//
//
function familyName($fname, $year) {
echo "$fname. Born in $year <br>";
}
familyName("Muhammad Ali", "1975");
familyName("Muhammad Nasir", "1978");
familyName("Muhammad Hamza", "1983");*/
//
// Example 4 - Default Argument Value
//
function setHeight($minheight = 50) {
echo "The height is : $minheight <br>";
}
setHeight(350);
setHeight(); // will use the default value of 50
setHeight(135);
setHeight(80);
//
// Example 5 - Returning Values
//
function sum($x, $y) {
$z = $x + $y;
return $z;
}
$i = sum(5, 10);
echo "5 + 10 = " . $i . "<br>";
echo "7 + 13 = " . sum(7, 13) . "<br>";
?>
</body>
</html>
Muhammad Yasir Bhutta