Web Based Application Using PHP (22619) : Mrs Priya Ramesh Rangdal (Zade)
Web Based Application Using PHP (22619) : Mrs Priya Ramesh Rangdal (Zade)
using PHP(22619)
Mrs Priya Ramesh Rangdal(zade)
Google classroom
PHP has many syntaxes similar to C, Java, and Perl, and has many unique features and specific functions.
PHP page is a file with a .php extension can contain a combination of HTML Tags and PHP scripts.
PHP recursive acronym for PHP(Hypertext Preprocessor): HyperText means, text containing all sorts of
web markups, PreProcessor means all of the HyperText is processed first and then the result is sent as pure
HTML to the web browser. A client cannot see the PHP source code because it is preprocessed and interpreted.
PHP is Server-side scripting language: Server-side scripting means that the PHP code is processed on the web
server rather than the client machine.
PHP is designed to work with HTML, so you can easily write and embed PHP code
with HTML. A PHP code block begins with <?php tag and ends with ?> tag.
Syntax:
<?php //your php code goes here
?>
PHP with Html
<html>
<title>Hello World program in PHP</title>
<body>
<? php echo "Hello World";
?>
</body>
</html>
To run this PHP program, you need to write above code in any text
editor and save it to your web server under www directory with
a .php extension.
Once it's done, start the server and go to localhost and type in the
path of your file, i.e., http://localhost/HelloWorld.php
Comments in PHP
The PHP interpreter ignores the comment block; it can be used anywhere in the
program to add info about program or code block, which can be helpful for the
programmer to understand the code easily in the future.
In PHP, we can use // or # to make a single-line comment, and /* and */ to
make a large comment block.
Example:
<html> <body>
<?php
// This is single line commented text, and
# This is another single line commented text
/* This is a Multi-lines comment block area *
/?> </body> </html>
https://www.youtube.com/watch?v=tlfVuqTQvx8
PHP variables
<?php
function testFunction()
{
echo "It works.";
}
$funct = "testFunction"; $funct(); // will call testFunction();
?>
Variable of variables are very potent, and variable should be used with
care because it can make complicated to understand and document
your code, but also because of their improper use may lead to critical
security issues
PHP data types
Variables can store data of different types, and different data types can do different
things.
PHP supports the following data types:
String
Integer
Float (floating point numbers - also called double)
Boolean
Array
Object
NULL
Resource
PHP string
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;
?>
Output:
Hello world!
Hello world!
PHP Integer
PHP Array
An array stores multiple values in one single variable.
In the following example $cars is an array. The PHP var_dump()
function returns the data type and value:
Example
<?php
$cars = array("Volvo","BMW","Toyota");
var_dump($cars);
?>
PHP objects
Classes and objects are the two main aspects of object-oriented programming.
A class is a template for objects, and an object is an instance of a class.
When the individual objects are created, they inherit all the properties and
behaviors from the class, but each object will have different values for the
properties.
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.
If you create a __construct() function, PHP will automatically call this
function when you create an object from a class.
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();
?>
Output
My car is a black Volvo!
My car is a red Toyota!
PHP null value
Null is a special data type which can have only one value: NULL.
A variable of data type NULL is a variable that has no value assigned to it.
Tip: If a variable is created without a value, it is automatically assigned a value of
NULL.
Variables can also be emptied by setting the value to NULL:
Example
<?php
$x = "Hello world!";
$x = null;
var_dump($x);
?>
Output
NULL
PHP Resource
=== Identical $x === $y Returns true if $x is equal to $y, and they are of the same type Try it »
!== Not identical $x !== $y Returns true if $x is not equal to $y, or they are not of the same Try it »
type
>= Greater than or equal to $x >= $y Returns true if $x is greater than or equal to $y Try it »
<= Less than or equal to $x <= $y Returns true if $x is less than or equal to $y Try it »
<=> Spaceship $x <=> $y Returns an integer less than, equal to, or greater than zero,
depending on if $x is less than, equal to, or greater than $y.
Introduced in PHP 7.
PHP Increment / Decrement Operators