Learn PHP
Learn PHP
<!DOCTYPE html>
<html>
<body>
<?php
$txt = "PHP";
?>
</body>
</html>
I love PHP!
2.
<!DOCTYPE html>
<html>
<body>
<?php
?>
<body>
<html>
My first PHP page
Hello World!
3.
<!DOCTYPE html>
<html>
<body>
<?php
?>
</body>
</html>
Hello World!
Hello Wolrld
Hello World
4.
<!DOCTYPE html>
<html>
<body>
<?php
$color = "red";
?>
</body>
</html>
My car is red
My house is red
My boat is red
5.
<form action="welcome.php" method="post">
</form>
<html>
<body>
$_POST["fname"]
; ?>
</body>
</html>
First name:
Welcome
6.
<!DOCTYPE html>
<html>
<body>
<?php
echo date(
"Y.m.d"
);
?>
</body>
</html>
2021.05.17
7.
<!DOCTYPE html>
<html>
<body>
<?php
?>
</body>
</html>
8.
<!DOCTYPE html>
<html>
<body>
<?php
$x = 5 /* + 15 */ +5
echo $x;
?>
</body>
</html>
10
9.
<!DOCTYPE html>
<html>
<body>
<?php
$x = 5;
$y = 10.5;
echo $txt;
echo "<br>";
echo $x;
echo "<br>";
echo $y;
?>
Hello world!
5
10.5
10.
<!DOCTYPE html>
<html>
<body>
<?php
$txt = "W3Schools.com";
?>
</body>
</html>
<?php
$txt = "W3SCHOOLS.COM";
I love W3Schools.com!
I love W3SCHOOLS.COM!
11.
<!DOCTYPE html>
<html>
<body>
<?php
$x = 5;
$y = 4;
echo $x + $y;
?>
<?php
$x = 3;
$y = 2;
echo $x +$y;
?>
</body>
</html>
95
12.
PHP Variables Scope
In PHP, variables can be declared anywhere in the script.
The scope of a variable is the part of the script where the variable can be
referenced/used.
local
global
static
Example
Variable with global scope:
<?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();
Example
Variable with local scope:
<?php
function myTest() {
$x = 5; // local scope
echo "<p>Variable x inside function is: $x</p>";
}
myTest();
13.
Example
<?php
function myTest() {
static $x = 0;
echo $x;
$x++;
}
myTest();
myTest();
myTest();
?>
0
1
2
3
14.
<!DOCTYPE html>
<html>
<body>
<?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.";
?>
<?php
echo "<h2>PHP IS FUN</h2>";
echo "Hello world!<br>";
echo "im about to learn php!<br>";
echo "this ", "string ", "was ", "made ", "with multiple parameters.";
?>
</body>
</html>
PHP is Fun!
Hello world!
I'm about to learn PHP!
This string was made with multiple parameters.
PHP IS FUN
Hello world!
im about to learn php!
this string was made with multiple parameters.
15.
<?php
$txt1 = "learn php";
$txt2 = "w3schools.com";
$x = 5;
$y = 4;
echo "<h2>".$txt1."</h2>";
echo "Study php at ".$txt2."<br>";
echo $x + $y;
?>
learn php
Study php at w3schools.com
9
16.
<?php
print "<h2>PHP is Fun!</h2>";
print "Hello world!<br>";
print "I'm about to learn PHP!";
?>
PHP is Fun!
Hello world!
I'm about to learn PHP!
17.
String
Integer
Float (floating point numbers - also called double)
Boolean
Array
Object
NULL
Resource
PHP String
A string is a sequence of characters, like "Hello world!".
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;
?>
Try it Yourself »
PHP Integer
An integer data type is a non-decimal number between -2,147,483,648 and
2,147,483,647.
Example
<?php
$x = 5985;
var_dump($x);
?>
Ans:
5985
18.
PHP Object
Classes and objects are the two main aspects of object-oriented
programming.
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.
Example
<?php
class Car {
public $color;
public $model;
public function __construct($color, $model) {
$this->color = $color;
$this->model = $model;
}
public function message() {
return "My car is a " . $this->color . " " . $this->model . "!";
}
}
$myCar = new Car("black", "Volvo");
echo $myCar -> message();
echo "<br>";
$myCar = new Car("red", "Toyota");
echo $myCar -> message();
?>
19.