PHP Tutorials for Beginners
No coding experience? No problem! Our PHP tutorials are designed for beginners. Learn PHP step-by-step and build your web development skills.
PHP (HypertextPreprocessor) is server-side scripting language designed for web development but also used as a general-purpose programming language. PHP is the most popular scripting language on the web.
Without PHP Facebook, Yahoo, Google wouldn’t have exist. The course is geared to make you a PHP pro. Once you digest all basics, the course will help you create your very own Opinion Poll application.
Website Development Company in Pakistan
PHP Syntax
PHP scripts are executed on the server and the result is returned to the browser as plain HTML.
<?php
echo”Hello,World!”;
?>
Comments
Comments are ignored by the PHP engine.
<?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
*/
?>
Variables
Variables in PHP start with the $sign, followed by the name of the variable.
<?php
$txt=”Hello,World!”;
$number=123;
//practical
<?php
?>
Constant….
<?php
define(“MESSAGE”,”Hello”,false); //case sensitive
echo MESSAGE;
echo message;
?>
<?php
const MESSAGE=”Hello “;
echo MESSAGE;
?>
DataTypes
PHP supports several data types including strings, integers, floats,booleans,arrays,objects,NULL,andresources.
Strings
A string is a sequence of characters.
<?php
$string=”Hello,World!”;
Echo $string;
?>
Integers
An integer is a non-decimal number between-2,147,483,648and2,147,483,647.
<?php
$int=123;
Echo $int;
?>
Floats
A float is a number with a decimal point or a num berin exponential form.
<?php
$float=12.34;
Echo $float;
?>
Booleans
A Boolean represents two possible states:TRUEorFALSE.
<?php
$bool=true;
Echo $bool;//Outputs1
?>
Arrays
An array is a data structure that stores one or more similar type of values in a single value.
IndexedArrays
<?php
$cars=array(“Volvo”,”BMW”,”Toyota”);
Echo $cars [0]; //Outputs”Volvo”
?>
<?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>”;
?>
//object
<?php
class bike {
function model() {
$model_name = “Royal Enfield”;
echo “Bike Model: ” .$model_name;
}
}
$obj = new bike();
$obj -> model();
?>
Associative Arrays
<?php
$ages=array(“Peter”=>35,”Ben”=>37,”Joe”=>43);
Echo $ages [‘Peter’]; //Outputs35
?>
Conditional Statements
If Statement
<?php
$time=10;
if($time<12){
echo”Goodmorning!”;
}
?>
if-else Statement
<?php
$time=20;
if($time<12){
echo”Goodmorning!”;
}else{
echo”Goodevening!”;
}
?>
if-else-if-else Statement
<?php
$time=15;
if($time<12){
echo”Goodmorning!”;
}elseif($time<18){
echo”Goodafternoon!”;
}else{
echo”Goodevening!”;
}
?>
Switch Statement
<?php
$day=”Monday”;
switch($day){
case”Monday”:
echo”It’sMonday!”;
break;
case”Tuesday”:
echo”It’sTuesday!”;
break;
default:
echo”It’s neither Monday nor Tuesday!”;
}
?>
Loops
While Loop
<?php
$i=1;
while($i<=5){
echo”Thenumberis:$i<br>”;
$i++;
}
?>
do-while Loop
<?php
$i=1;
do{
echo”The numberis:$i<br>”;
$i++;
}while($i<=5);
?>
forLoop
<?php
for($i=1;$i<=5;$i++){
echo”Thenumberis:$i<br>”;
}
?>
For each Loop
<?php
$colors=array(“red”,”green”,”blue”,”yellow”);
foreach($colorsas$color){
echo”$color<br>”;
}
?>
Functions
A function is a block of statements that can be use repeatedly in a program.
<?php
functionwriteMsg(){
echo”Hello,World!”;
}
writeMsg();
?>
Functions with Parameters
<?php
functionfamilyName($fname){
echo”$fnameRefsnes.<br>”;
}
familyName(“Jani”);
familyName(“Hege”);
familyName(“Stale”);
?>
Functions with Return Values
<?php
functionadd($x,$y){
$sum=$x+$y;
return$sum;
}
echoadd(5,10);
?>
Super globals
PHP super global are built-in variables that are always available in all scopes.
$_GET
<!–HTMLform–>
<formmethod=”get”action=”welcome.php”>
Name:<inputtype=”text”name=”name”>
<inputtype=”submit”>
</form>
<!–PHPscript(welcome.php)–>
<?php
echo”Welcome”.$_GET[‘name’];
?>
$_POST
<!–HTMLform–>
<formmethod=”post”action=”welcome.php”>
Name:<inputtype=”text”name=”name”>
<inputtype=”submit”>
</form>
<!–PHPscript(welcome.php)–>
<?php
echo”Welcome”.$_POST[‘name’];
?>
$_SERVER
<?php
echo$_SERVER[‘PHP_SELF’];
echo”<br>”;
echo$_SERVER[‘SERVER_NAME’];
echo”<br>”;
echo$_SERVER[‘HTTP_HOST’];
echo”<br>”;
echo$_SERVER[‘HTTP_USER_AGENT’];
echo”<br>”;
echo$_SERVER[‘SCRIPT_NAME’];
?>
Form Handling
Forms are used to collect user inputs.
Simple Form Handling
<!–HTMLform–>
<formmethod=”post”action=”<?phpecho$_SERVER[‘PHP_SELF’];?>”>
Name:<inputtype=”text”name=”name”>
<inputtype=”submit”>
</form>
<!–PHPscript–>
<?php
if($_SERVER[“REQUEST_METHOD”]==”POST”){
$name=htmlspecialchars($_POST[‘name’]);
echo”Welcome,$name!”;
}
?>
FileHandling
PHP allows you to open, read, write, and close files on the server.
ReadingaFile
<?php
$file=fopen(“test.txt”,”r”)ordie(“Unabletoopenfile!”);
echofread($file,filesize(“test.txt”));
fclose($file);
?>
Writing to a File
<?php
$file=fopen(“test.txt”,”w”)ordie(“Unabletoopenfile!”);
$txt=”JohnDoe\n”;
fwrite($file,$txt);
$txt=”JaneDoe\n”;
fwrite($file,$txt);
fclose($file);
?>
Include and Require
They include and require statements are used to include the content to fine PHP file into another PHP file.
include
<?php
include’footer.php’;
?>
require
<?php
require’header.php’;
?>
Database Connection
Connecting to a MySQL database using PHP.
MySQLi Object-Oriented
<?php
$servername=”localhost”;
$username=”username”;
$password=”password”;
$dbname=”myDB”;
//Create connection
$conn=newmysqli($servername,$username,$password,$dbname);
//Check connection
if($conn->connect_error){
die(“Connectionfailed:”.$conn->connect_error);
}
echo”Connected successfully”;
?>
MySQLi Procedural
<?php
$servername=”localhost”;
$username=”username”;
$password=”password”;
$dbname=”myDB”;
//Create connection
$conn=mysqli_connect($servername,$username,$password,$dbname);
//Check connection
if(!$conn){
die(“Connectionfailed:”.mysqli_connect_error());
}
echo”Connected successfully”;
?>
PD
<?php
$servername=”localhost”;
$username=”username”;
$password=”password”;
try{
$conn=newPDO(“mysql:host=$servername;dbname=myDB”,$username,$password);
//setthe PDO error mode to exception
$conn->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
echo”Connected successfully”;
}catch(PDO Exception$e){
echo”Connectionfailed:”.$e->getMessage();
}
?>
PHP Tutorial for beginners and professionals