PHP-notes
PHP-notes
Server-side scripting is a coding method for designing websites that may run software on the
server side to make dynamic pages.This technique forms the basic framework for back-end
web development to execute program codes at the server end. Some of the most
commonexamples are:Python, Perl, Ruby, PHP etc.
Introduction to PHP
PHP (Hypertext Preprocessor), is popular server-side scripting language, developed by Danish-
Canadian programmer Rasmus Lerdorffor web purposes.
Features of PHP
Interpreted language
Flexibility
Object oriented
Free and open-source
Case-sensitive
Limitations of PHP
Security issues
Extra learning
Lack of error handling
PHP File
A PHP file is like a plain-text file which may contain JavaScript, HTML, CSS and numerous
PHP codes written in the PHP programming language. The default extension of PHP file
is .PHP.
PHP tags
Types of PHP tags
1. Canonical PHP tags
<?php>PHP tag </?>
2. Short-open (SGML-style) tags
<?.............................................?>.
3. ASP(Active Server Page) -style tags
. <%..........................%>
4. HTML script tags
<script language = "PHP">…………………………..</script>
PHP Variables
Variables in PHP are the containers or memory location for storing data and information. PHP
variable is started with a dollar ($) sign.
Syntax
$variablename=value;
$num=10;
PHP variable manipulation (Variable Variables)
Variable manipulation (Variable variables) are the special features of PHP which supports to store
the name of a variable into a variable.
Example
<html><body>
<?php
$name="Tiger";
${$name}="Buffalo";
${${$name}}="Monkey";
echo $name "<br>";
echo ${$name} "<br>";
echo ${${$name}} "<br>";
?></body></html>
Types of PHP variable
a. Local Variable
b. Global Variable
Generally, global variables in PHP can be defined by using the keyword global.
c. Super global variables
They are special types of variables making them "super" in terms of their global reach which are prefixed
with a dollar sign ($) followed by an underscore (_).
Super global variables Description
$GLOBALS Used to assign or access any variable data globally.
$_POST Used after submitting a form which method type is POST.
$_GET Used after submitting a form which method type is GET.
$_REQUEST Used for both GET and POST.
$_SERVER Used to access all server data easily.
$_SESSION Used to one time browser tab close.
PHP data types
Integers: whole numbers, without decimal point, like 4195.
Doubles: floating-point numbers, like 3.14159 or 49.1.
Strings: sequences of characters
Arrays: named and indexed collections of other values.
Objects: instances of programmer-defined classes
PHP basic statements
Echo statement
The echo is used to display anything as output including string, numbers, variables values, the
results of expressions etc. to the browser.
PHP simple program to calculate and display sum of 100 and 200.
<html>
<body>
<?php
$txt1 = "Sum of 100 and 200 is:";
$x = 100;
$y = 200;
echo"<h3>" . $txt1 ."</h3>";
echo$x + $y;
?>
</body></html>
Print Statement
The print statement is also used to display output to the browser.
Example
<html><body><?php
$txt = "Hello Computer";
$num = 123456789;
$colors = array ("Red", "Green", "Blue");
print $txt;
print "<br>";
print $num;
print "<br>";
print $colors[0];
?></body></html>
PHP Functions
PHP code to calculate and display sum of 10 and 20 using function.
<html>
<body>
<?php
//creating a function
functionadd($a,$b)
{
$c=$a+$b;
echo$c;
}
echo"sum of 10 and 20 using function is ";
//calling a function
add(10, 20);
?>
</body></html>
PHP Array
PHP code to sort and display numbers in ascending order.
<html>
<body>
<?php
$numbers=array(40,16,20,60,30);
echo"The numbers in ascending order<br>";
sort($numbers);
foreach($numbers as $n )
{
echo"$n<br >";
}
?></body></html>