PHP Notes

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 26

PHP

 created by Danish-Canadian programmed by Rasmus Lerdorf in 1994.


 PHP is a general-purpose, widely-used, open source scripting language for web development.
 PHP is an acronym for "PHP: Hypertext Preprocessor.
 PHP scripts are executed on the server.
 PHP is free to download and use
 PHP is open source.
 It is platform independent.
 It loads faster on even slower connection.It is light weighted.
 It’s built-in database connection modules are very capable.

Define and Discriminate


 Client side scripting.
o A client-side script is a program that is processed within the client browser. These kinds
of scripts are small programs which are downloaded , compiled and run by the browser.
JavaScript is an important client-side scripting language and widely used in dynamic
websites.
 Examples:-React,css,JavaScript,Angular,HTML,Vue,JQuery,Swift etc

 Server side scripting.


o Server side scripting is a process in which server data formats into an HTML response
before it is sent back to the web browser.
o In other words, a process in which the script code is executed by the web server on the
server side after the page is requested is called server side scripting.
 Examples:-PHP, Python, Ruby, C#, Perl,ASP and JavaScript (NodeJS).

 Middle ware
o software that acts as a bridge between an operating system or database and
applications, especially on a network.

 Web server
o A web server is software and hardware that uses HTTP (Hypertext Transfer Protocol)
and other protocols to respond to client requests made over the World Wide Web.
o The term web server can refer to hardware or software, or both of them working
together.
o On the hardware side, a web server is a computer that stores web server software and a
website's component files (for example, HTML documents, images, CSS stylesheets, and
JavaScript files). A web server connects to the Internet and supports physical data
interchange with other devices connected to the web.
o On the software side, a web server includes several parts that control how web users
access hosted files. At a minimum, this is an HTTP server. An HTTP server is software
that understands URLs (web addresses) and HTTP (the protocol your browser uses to
view webpages). An HTTP server can be accessed through the domain names of the
websites it stores, and it delivers the content of these hosted websites to the end user's
device.
 Nginx
 IIS
 lighttpd
 Apache Tomcat
 LiteSpeed Web Server
 Oracle WebLogic Server
 Oracle iPlanet Web Server
 Jigsaw
 Apache Traffic Server
 Google Web Server
 IBM HTTP Server
 Application server
 XAMPP
 WAMP

 PHP Parser − In order to process PHP script instructions a parser must be installed to generate
HTML output that can be sent to the Web Browser. This tutorial will guide you how to install
PHP parser on your computer.
Languages/Tools to develop web applications.

Languages:
 Java
 Python
 PHP
 C#
 Ruby
 Pearl
 Now is the time of Multilingual web services.

Tools:
 Angular
 Wordpress
 React
 Laravel
 Bootstrap
 Django etc.

PHP is used in developing


 Content Management System.
 Web-based applications and sites.
 E-commerce websites and apps.
 Data Analytics and Representation.
 Processing of Images.
 Graphical interface design.

And,
 It is commonly used to extract data out of a database on the Web server and present it on the
Web page.
 It is used to manage
o dynamic content,
o databases,
o session tracking,
o even build entire e-commerce sites.
PHP, the Characteristics of it
 Simplicity.
o It has less learning curve, because it is simple and straightforward to use. Someone
familiar with C programming can easily work on PHP. 
 Efficiency
o It’s modules in all programming sections are considered to be very efficient.
 Security
 Flexibility
 Familiarity

<<end of the first class>>


Installing XAMPP

Step 1: Download
XAMPP is a release made available by the non-profit project Apache Friends. Versions
with PHP 5.5, 5.6, or 7 are available for download on the Apache Friends website.

Step 2:
Run .exe file

Once the software bundle has been downloaded, you can start the installation by
double clicking on the file with the ending .exe.

Step 3:
Deactivate any antivirus software

Before installing XAMPP, it is advisable to disable the anti-virus program temporarily

Step 4:
Deactivate UAC

User account control can affect the installation of XAMPP

Step 5:
Start the setup wizard

After you’ve opened the .exe file (after deactivating your antivirus program(s) and taken
note of the User Account Control, the start screen of the XAMPP setup wizard should
appear automatically. Click on ‘Next’ to configure the installation settings.

You can start the setup on the startup screen

Step 6:
Choose software components
Under ‘Select Components’, you have the option to exclude individual components of
the XAMPP software bundle from the installation. But for a full local test server, we
recommend you install using the standard setup and all available components. After
making your choice, click ‘Next’.

In the dialog window entitled 'select components', you can choose the software
components before installation

Step 7:
Choose the installation directory

In this next step, you have the chance to choose where you’d like the XAMPP software
packet to be installed. If you opt for the standard setup, then a folder with the name
XAMPP will be created under C:\ for you. After you’ve chosen a location, click ‘Next’.

For the next step, you need to select the directory where XAMPP should be installed

Step 8:
Start the installation process

Once all the aforementioned preferences have been decided, click to start the
installation. The setup wizard will unpack and install the selected components and save
them to the designated directory. This process can take several minutes in total. You can
follow the progress of this installation by keeping an eye on the green loading bar in the
middle of the screen.

According to the default settings, the selected software components are unpacked and
installed in the target folder

Step 9:
Windows Firewall blocking

Your Firewall may interrupt the installation process to block the some components of
the XAMPP. Use the corresponding check box to enable communication between the
Apache server and your private network or work network. Remember that making your
XAMPP server available for public networks isn’t recommended. 
Step 10:
Complete installation

Once all the components are unpacked and installed, you can close the setup wizard by
clicking on ‘Finish’. Click to tick the corresponding check box and open the XAMPP
Control Panel once the installation process is finished.

By clicking on 'finish', the XAMPP Setup Wizard is completed

The XAMPP Control Panel


Controls for the individual components of your test server can be reached through the XAMPP Control
Panel.

The XAMPP Control Panel also offers you various other buttons, including:

 Config: allows you to configure the XAMPP as well as the individual components
 Netstat: shows all running processes on the local computer
 Shell: opens a UNIX shell
 Explorer: opens the XAMPP folder in Windows Explorer
 Services: shows all services currently running in the background
 Help: offers links to user forums
 Quit: closes the XAMPP Control Panel

In the Control Panel, we can start and stop individual modules

Starting modules
Individual modules can be started or stopped on the XAMPP Control Panel through the corresponding
buttons under ‘Actions’. We can see which modules have been started because their names are
highlighted green under the ‘Module’ title.

An active module is marked in green in the Control Panel

If a module can’t be started as a result of an error, you’ll be informed of this straight away in red font. A
detailed error report can help you identify the cause of the issue. 

Setting up XAMPP
Configuring port

<<end of class 2>>


 To do:The folder structure of XAMPP installatation.
 To do:How to set the port in the httpd.conf file.
 To discuss;
o What if we have more than one installation of xampp.
o The default port
o Can we change the port
o Can we have both 32 bit installation and 64 bit installation
on the same machine.
-----------------------------------------------------------------------------------------------

 Data Types:
 String
 Integer
 Float (floating point numbers - also called double)
 Boolean
 Array
 Object
 NULL
 Resource

1. Scalar Types (predefined):-


1. boolean: It holds only two values: TRUE (1) or FALSE (0). It is often used with
conditional statements. If the condition is correct, it returns TRUE otherwise
FALSE

example:
a) $var=TRUE;
b) $gender="Male";

2. integer:

a) means numeric data with a negative or positive sign.


b) It holds only whole numbers, i.e., numbers without fractional part or
decimal points.
c) can be either positive or negative.
d)  must not contain decimal point.
e) between -2,147,483,648 and 2,147,483,647 

example:
$a = 123; var_dump($a); echo "<br>";
$b = -123; var_dump($b); echo "<br>";
3. float
a) Unlike integer, it can hold numbers with a fractional or decimal point,
b) including a negative or positive sign.

example: $n1 = 19.34;  

4. string
a) It holds letters or any alphabets, numbers, and even special characters.
Examples:
a. $aa = 'PHP string type';
b. $message = "PHP data types";

2. Compound Types (user-defined)


1. array:
a) is a compound data type.
b) It can store multiple values of same data type in a single variable.
2. object
a) are the instances of user-defined classes
b) these can store both values and functions. 
c)
3. resource,NULL are two Special type of data.
1. Resources are not the exact data type in PHP. Basically, these are used to store
some function calls or references to external PHP resources. For example - a
database call. It is an external resource.
2. Null is a special data type that has only one value: NULL. There is a convention of
writing it in capital letters as it is case sensitive.
------------------------------------------------------------------------------
Coding:
<?php
…………...
…………...
………..
?>
<html>
<body>
<h1>My first PHP page</h1>
<?php
echo "Hello World!";
?>
</body>
</html>

<<end of day three>>


Variable continues demo of different types of variables.
To do:
 Change the port setting and show it on the browser
 Write the first php example program and explain that.
To discuss:
some points to know about variables in PHP.
 All variables in PHP are denoted with a leading dollar sign ($).
 The value of a variable is the value of its most recent assignment.
 Variables are assigned with the = operator, with the variable on the left-hand
side and the expression to be evaluated on the right.
 not needed, to be declared before assignment.
 Variables in PHP do not have intrinsic types - a variable does not know in
advance whether it will be used to store a number or a string of characters.
 PHP automatically converts types from one to another when necessary.

<<end of class four>>


To discuss:
Types of array.
 Numeric array − An array with a numeric index. Values are stored and
accessed in linear fashion.
 Associative array − An array with strings as index. This stores element values
in association with key values rather than in a strict linear index order.
 Multidimensional array − An array containing one or more arrays and values
are accessed using multiple indices

 Syntax:
array(value1, value2, value3, etc.)

 Syntax: 
array(key=>value,key=>value,key=>value,…..)
Examples:
<?php
$nm = Array("Arvind","Kamal","Jakir","Shradha");
echo $nm[1];

$age = Array(1,2,3,4,5,6);
echo $age[3];

$ter= Array(1=>"Bihar",2=>"MP",3=>"UP",4=>"AP");
echo $ter[4];

$mx=Array("GOA","DELHI","Bihar","Bihar"=>"Patna","U
P"=>"Lakhnow","MP"=>"Bhopal");
echo $mx["UP"];

$points=Array("Ajay"=>Array("studies"=>12,"Sports",=
>15),"Vijay" =>Array("studies"=>31,"Sports"=>12));

echo $points['Ajay']['studies'];
?>
The count() function returns the number of elements in an array.
sizeof($arr): returns the size of the array or the number of data
elements stored in the array.
array_merge($arr1, $arr2)
array_values($arr)
array_keys($arr)
array_pop($arr)
array_push($arr, $val)
sort($arr)
array_flip($arr)
array_reverse($arr)

<<end of the class fifth>>


Sixth class
$nm = Array("Arvind","Kamal","Jakir","Shradha");
echo $nm[1]."<br>";
$ag = Array(1,2,3,4,5,6);
echo $ag[3]."<br>";
$ter= Array(1=>"Bihar",2=>"MP",3=>"UP",4=>"AP");
echo $ter[4]."<br>";
$mx=
Array("GOA","DELHI","Bihar","Bihar"=>"Patna","UP"=>"Lackhnow","MP"=>"Bh
opal");
echo $mx["UP"]."<br>";
$points=Array("Ajay"=>Array("studies"=>12,"Sports",15),"Vijay"=>Array("SW"
=>31,"AW"=>12));
echo $points['Ajay']['studies']."<br>";
echo sizeof($mx);
echo "<br><br>";
print_r(array_merge($nm, $ag));
echo "<br>";
print_r(array_values($nm));
echo "<br>";
print_r(array_values($ag));
echo "<br>";
print_r(array_flip($ag));
echo "<br>";
print_r(array_reverse($ag));
echo "<br>";
print_r(array_keys($nm));
echo "<br>";
print_r(array_pop($nm));
echo "<br>";
print_r($nm);
echo "<br>";
print_r(array_push($nm, "ZEDEK"));
echo "<br>";
print_r($nm);
print_r(sort($ag));
print_r($ag);

This is the code we have seen in the last class only.


Any confusion in this code?

Echo/print/var_dump
 echo is a statement, which is used to display the output.
 The echo() function is not actually a function, so you are not required to
use parentheses with it.
 In order to output one or more strings, we can use echo statement.
Anything that can be displayed to the browser using echo statement,
such as string, numbers, variables values, the results of
expressions, etc.
 It is required to use parenthesis if you want to use more than one
parameter.

 The print()outputs one or more strings.


 The print() function is not actually a function, so you are not
required to use parentheses with it.
 The print and echo are both language constructs to display strings.

 var_dump() displays values along with data types as output. 

Code to understand echo,print,print_r and var_dump

<?php
$a = array ('a' => 'apple', 'b' => 'banana', 'c' => array ('x', 'y', 'z'));
echo array($a);// returns Array
echo "<br>";
print_r($a); // returns what check this;
echo "<br>";
var_dump($a);
echo"<br>"."----------------------------","<br>";
$b=11;
echo $b."<br>";
print $b."<br>";
print_r($b)."<br>";
var_dump($b) .”<br>” ;
$name(“Ajay”,”Vijay”,”Kamal”,”Jayesh”,”Arvind”,”Ranjeet” );

?>
Array once again.
1. $season=array("summer","winter","spring","autumn");  
A. $season[0]="summer";  
B. $season[1]="winter";  
C. $season[2]="spring";  
D. $season[3]="autumn";  

<?php  
$season[0]="summer";  
$season[1]="winter";  
$season[2]="spring";  
$season[3]="autumn";  
echo "Season are: $season[0], $season[1], $season[2] and $season[3]";  
?>  
Declaring:One way of doing this in associative array

1. $salary=array("amit "=>"350000"," sumit "=>"450000","Kartik"=>"200000");  

Another way of doing this in associative array.

1. $salary["amit "]="350000";  
2. $salary["sumit "]="450000";  
3. $salary["Kartik"]="200000";  

Assigning:One way of doing this in associative array

1. $salary=array("amit "=>"350000","sumit"=>" 450000 ","Kartik"=>"200000");  


Another way of doing this in associative array.

1. $salary["amit"]="350000";  
2. $salary["sumit"]="450000";  
3. $salary["kaif"]="200000";  

Examples in the above explained 2 examples.


<?php    
$salary=array("amit"=>"350000","sumit"=>"450000","kaif"=>"200000");    
echo "Amit salary: ".$salary["amit"]."<br/>";  
echo "Sumit salary: ".$salary["sumit"]."<br/>";  
echo "Kaif salary: ".$salary["kaif"]."<br/>";  
?>    
<?php    
$salary["amit "]="350000";    
$salary["sumit "]="450000";    
$salary["Kartik"]="200000";    
echo " amit salary: ".$salary["amit "]."<br/>";  
echo " sumit salary: ".$salary["sumit "]."<br/>";  
echo " kaif salary: ".$salary["kaif "]."<br/>";  
?>    

Operators
Their precedence and assosiativity:

Operator precedence determines which operator is performed first in


an expression with more than one operators. 

 *, /, % operators have equal precedence.
 + , - operators have equal precedence.
 *, / , % have a higher precedence than + , -.
Left Associativity 
Left associativity occurs when an expression is evaluated from left to
right.
100 / 10 * 10  is treated as (100 / 10) * 10

Right Associativity 

Right associativity occurs when an expression is evaluated from right


to left. Example is:  =
Example:

(5∗3)%2 gives an output 1


(5%3)∗2 gives an output 4

o Arithmetic Operators
o + Addition $a + $b Sum of operands
o - Subtraction $a - $b Difference of operands
o * Multiplication $a * $b Product of operands
o / Division $a / $b Quotient of operands
o % Modulus $a % $b Remainder of operands
o ** Exponentiation $a ** $b $a raised to the power $b

o Assignment Operators
o Bitwise Operators
o Comparison Operators
o Incrementing/Decrementing Operators
o Logical Operators
o String Operators
o Array Operators
o Type Operators
o Execution Operators

o = Assign $a = $b
The value of right operand is assigned to the left operand.
o += Add then Assign $a += $b
Addition same as $a = $a + $b
o -= Subtract then Assign $a -= $b
Subtraction same as $a = $a - $b
o *= Multiply then Assign $a *= $b
Multiplication same as $a = $a * $b
o /= Divide then Assign
(quotient) $a /= $b Find quotient same as $a = $a / $b
o %= Divide then Assign
(remainder) $a %= $b Find remainder same as $a = $a % $b

<<end of sixth class>>


<<Seventh Class>>22/3/22/3/2/23
. <Definition of these operators should be known to you>
<Still here is a small detail.>
<If you don’t have the basic idea of operators. ask >
 To Discuss

A=10 and B=20


Arithmetic Operator Increment operator, increases
+ integer value by one
Adds two operands A++ will give 11
A + B will give 30
--
- Decrement operator,
Subtracts second operand decreases integer value by
from the first one
A - B will give -10 A-- will give 9

* Comparision Operator:
Multiply both operands
A * B will give 200 == Checks if the value of
two operands are equal or
/ not, if yes then condition
Divide numerator by de- becomes true.
numerator (A == B) is not true.
B / A will give 2
!= Checks if the value of
% two operands are equal or
Modulus Operator and not, if values are not equal
remainder of after an integer then condition becomes true.
division (A != B) is true.
B % A will give 0
> Checks if the value of
++ left operand is greater than
the value of right operand, if operands are non zero then
yes then condition becomes condition becomes true.
true. (A or B) is true.
(A > B) is not true.
&& Called Logical AND
< Checks if the value of operator. If both the
left operand is less than the operands are non zero then
value of right operand, if yes condition becomes true.
then condition becomes true. (A && B) is true.
(A < B) is true.
|| Called Logical OR
>= Checks if the value of Operator. If any of the two
left operand is greater than or operands are non zero then
equal to the value of right condition becomes true.
operand, if yes then condition (A || B) is true.
becomes true.
(A >= B) is not true. ! Called Logical NOT
Operator. Use to reverses the
<= Checks if the value of logical state of its operand. If
left operand is less than or a condition is true then
equal to the value of right Logical NOT operator will
operand, if yes then condition make false.
becomes true. !(A && B) is false.
(A <= B) is true.
Assignment Operator:
Logical Operator = Simple assignment
operator, Assigns values from
and Called Logical AND right side operands to left
operator. If both the side operand
operands are true then C = A + B will assign value of
condition becomes true. A + B into C
(A and B) is true.
+= Add AND assignment
or Called Logical OR operator, It adds right
Operator. If any of the two operand to the left operand
and assign the result to left C *= A is equivalent to C = C
operand *A
C += A is equivalent to C = C
+A /= Divide AND assignment
operator, It divides left
-= Subtract AND operand with the right
assignment operator, It operand and assign the result
subtracts right operand from to left operand
the left operand and assign C /= A is equivalent to C = C /
the result to left operand A
C -= A is equivalent to C = C -
A %= Modulus AND
assignment operator, It takes
*= Multiply AND modulus using two operands
assignment operator, It and assign the result to left
multiplies right operand with operand
the left operand and assign C %= A is equivalent to C = C
the result to left operand %A

<You must be knowing it as Ternary operator in c/c++>

If Condition is true ? Then


value X : Otherwise value Y
?:
x>y?11:12
Some other operators as mentioned above (apart of some
these):
<Precedence and Associativity is already discussed>

Array Operator:
1) + a+b Union Union of a and b.
2) == a==b Equality TRUE if a and b ,same key/value pairs.
3) === a===b Identity TRUE if a and b have the same key/value
pairs in the same order and of the same types.
4) != a!=b Inequality TRUE if aisnotequaltob.
5) <> a<>b Inequality TRUE if aisnotequaltob.
6) !== a!==b Non-identity TRUE if aisnotidenticaltob.

String operator
.
.=
$a = "Hello ";
$b = $a . "World!"; 
Now $b = "Hello World!"

$a = "Hello ";
$a .= "World!";    
now $a = "Hello World!"

Decision Making in PHP:


<if you remember it is all same as the constructs you know from c/C++,still

any confusion ask>


 If……else if(…….){……}else{….}
 If …… elseif …… else
 Switch….. case
Break is also used in the same way

Looping Construct in PHP:


<this also is almost same as c/C++ but foreach we will check it once>
 while
 do … while
 for
 foreach
 To do

<This code shows you how to write while,for,fornext)


<?php
$city=Array('Delhi','Chennai','Mumbai','Kolkata');
$cc=count($city);
$b=0;
while($b<$cc)
{
echo $city[$b]."<br>";
$b++;
}
echo "-------------------------------------<br>";
for($b=0;$b<count($city);$b++)
{
echo $city[$b]."<br>";
}
echo "-------------------------------------<br>";
foreach($city as $c)
echo "Hello ".$c."<br>";
echo "-------------------------------------<br>";
$statencap=array("Bihar"=>"Patna","Up"=>"Lukhnow","Maharastra"=>"Mumb
ai","WestBengal"=>"Kolkata");
foreach($statencap as $sp=>$cap)
echo "The state is ".$sp ." and the capital is:".$cap."<br>";
echo "-------------------------------------<br>";
?>

<any confusion ask it now>


<<coding practice>>
$x=11; }
$y="11"; echo
var_dump($x===$y); "-------------------------------------
echo "<br>"; <br>";
echo
"------------------------------------- foreach($city as $c)
<br>"; echo "Hello ".$c."<br>";
echo "<br>";
echo
$nn="Ranchi"; "-------------------------------------
echo $nn."Patna"."<br>"; <br>";
echo "$nn"."Patna"."<br>";
$nn.="Patna"; $statencap=array("Bihar"=>"Patna",
echo $nn."<br>"; "Up"=>"Lukhnow","Maharastra"=>"
echo Mumbai","WestBengal"=>"Kolkata")
"------------------------------------- ;
<br>"; foreach($statencap as $sp=>$cap)
echo "The state is ".$sp ." and the
capital is:".$cap."<br>";
$city=Array('Delhi','Chennai','Mumb
echo
ai','Kolkata');
"-------------------------------------
$cc=count($city);
<br>";
$b=0;
while($b<$cc)
$n1=array(1,2,3,array(11,22,33),4,arr
{
ay(55,66));
echo $city[$b]."<br>";
echo
$b++;
"<table><tr><th>Numbers</th><
}
/tr>";
echo
foreach($n1 as $n2=>$v1)
"-------------------------------------
{
<br>";
echo "<tr><td bgcolor=red>";
$cc=count($v1);
for($b=0;$b<count($city);$b++)
if($cc>1)
{
{
echo $city[$b]."<br>";
foreach($v1 as $w1)
{
echo "<td bgcolor=green>";
echo $w1."<br>";
echo "</td>";
}
}
else
echo $v1."<br>";
echo "</td>";
echo "</tr>";
}
echo
"-------------------------------------
<br>";

?>

You might also like