basics-php
basics-php
md 12/19/2022
PHP
updated on: 19-Dec-2022
PHP Basics
Download PDF
Download examples Codes
Tools
Visual Studio Code is a code editor redefined and optimized for building and debugging modern web and
cloud applications.
https://code.visualstudio.com/
Visual Studio Code - PHP Extension Pack
https://www.tutorialspoint.com/execute_php_online.php
http://phpfiddle.org/ PhpFiddle provides Web IDE and execution environment for PHP/MySQL, and
HTML/CSS/JavaScript coding online.
http://phptester.net/ Online PHP code tester
What is PHP
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.
PHP files can contain text, HTML, CSS, JavaScript, and PHP code.
PHP code are executed on the server, and the result is returned to the browser as plain HTML.
PHP files have extension ".php".
1 / 11
basics-php.md 12/19/2022
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?
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>
2 / 11
basics-php.md 12/19/2022
<?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
Variables
Variables are used to store data, like string of text, numbers, etc.
Variable values can change over the course of a script.
In PHP, a variable does not need to be declared before adding a value to it.
PHP automatically converts the variable to the correct data type, depending on its value
After declaring a variable it can be reused throughout the code.
The assignment operator (=) used to assign value to a variable.
all variable names are case-sensitive.
<?php
$color = "red";
$COLOR="GREEN";
echo "My car is " . $color . "<br>";
echo "My house is " . $COLOR . "<br>";
echo "My boat is " . $coLOR . "<br>";
?>
3 / 11
basics-php.md 12/19/2022
<?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;
?>
Variable scope
<?php
function myTest() {
$x = 5; // local scope
echo "<p>Variable x inside function is: $x</p>";
}
4 / 11
basics-php.md 12/19/2022
myTest();
<?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();
<?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
?>
5 / 11
basics-php.md 12/19/2022
$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
?>
A static variable exists only in a local function scope, but it does not lose its value when program
execution leaves this scope.
<?php
function test()
{
$a = 0;
echo $a;
$a++;
}
?>
<?php
function myTest() {
static $x = 0;
echo $x . "<br \>";
$x++;
}
6 / 11
basics-php.md 12/19/2022
myTest();
myTest();
myTest();
myTest();
?>
String functions
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
7 / 11
basics-php.md 12/19/2022
<?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!
?>
Comments
<!DOCTYPE html>
<html>
<body>
<?php
// This is 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>
8 / 11
basics-php.md 12/19/2022
define function
<!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>
functions
<!DOCTYPE html>
<html lang="en">
<head>
<title>PHP - Functions</title>
</head>
<body>
<?php
// Syntax
//
/*function functionName() {
code to be executed;
}*/
//
// Example1
//
9 / 11
basics-php.md 12/19/2022
function writeMsg() {
echo "Hello world!";
}
//
// Example2 - By Argument
//
function familyName($fname) {
echo "$fname Nasir" . "<br>";
}
familyName("Muhammad"); // call
familyName("Ali");
familyName("Zeeshan");
//
// Example 4 - Default Argument Value
//
setHeight(350);
setHeight(); // will use the default value of 50
setHeight(135);
setHeight(80);
//
// Example 5 - Returning Values
//
10 / 11
basics-php.md 12/19/2022
?>
</body>
</html>
References
What is PHP?
PHP Tutorial," w3schools
PHP Manual
Web
Youtube
Facebook
Twitter
11 / 11