0% found this document useful (0 votes)
2 views

PHP_NOTES1.1

The document provides an overview of PHP, a powerful server-side scripting language used for web development, including its history, syntax, and key features such as variables, data types, operators, control structures, and functions. It also covers common combinations of platforms and programs that utilize PHP, as well as form handling and validation techniques. Examples of PHP code snippets illustrate various concepts, making it a comprehensive guide for understanding and using PHP effectively.

Uploaded by

saahu779
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

PHP_NOTES1.1

The document provides an overview of PHP, a powerful server-side scripting language used for web development, including its history, syntax, and key features such as variables, data types, operators, control structures, and functions. It also covers common combinations of platforms and programs that utilize PHP, as well as form handling and validation techniques. Examples of PHP code snippets illustrate various concepts, making it a comprehensive guide for understanding and using PHP effectively.

Uploaded by

saahu779
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

Common Combination of Platform and Programs

 LAMP – Linux Apache MySQL and PHP


 WAMP – Windows Apache MySQL and PHP
 MAMP – Mac Apache MySQL and PHP
 XAMPP – Cross Platform Apache MySQL PHP and PERL

PHP is a general-purpose and powerful scripting language to produce dynamic


website and interactive web pages.

What is PHP?
 PHP is a sever side scripting language.
 PHP is interpreted and runs on a web server.
 PHP differs from other scripting language such as Javascript that runs on the
web browser, since it runs on a web server.
 PHP is designed to use alongside HTML, it can be embedded inside HTML or it
can also work with HTML and it returns to the web browser as HTML for end
user viewing.

History of PHP
PHP, known originally as Personal Home Pages, was first conceived in the
autumn of 1994 by Rasmus Lerdorf. After named PHP: Hypertext Preprocessor

PHP as a Language
 PHP is a powerful server-side scripting language for creating dynamic and
interactive websites.
 PHP is the widely-used, free, and efficient alternative to competitors such as
Microsoft's ASP. PHP is perfectly suited for Web development and can be
embedded directly into the HTML code.
 The PHP syntax is very similar to Perl and C. PHP is often used together with
Apache (web server) on various operating systems.

Syntax:
A PHP scripting block always starts with
<?php
and ends with
?>.

A PHP scripting block can be placed anywhere in the document. On servers with
shorthand support enabled you can start a scripting block with
<? and end with ?>.

Example:
<html>
<body>

<?php
echo "Hello World";
?>
</body>
</html>

Each code line in PHP must end with a semicolon. The semicolon is a separator and
is used to distinguish one set of instructions from another.

There are two basic statements to output text with PHP: echo and print.

Comments in PHP
In PHP, we use // to make a single-line comment or /* and */ to make a large
comment block.

Example:
<html>
<body>

<?php
//This is a comment
/* This is a comment block */
?>

</body>
</html>

VARIABLES

Rules for PHP variables:

 A variable starts with the $ sign, followed by the name of the variable

 A variable name must start with a letter or the underscore character

 A variable name cannot start with a number


 A variable name can only contain alpha-numeric characters and underscores

(A-z, 0-9, and _ )

 Variable names are case-sensitive ($age and $AGE are two different

variables)

*** PHP variable names are case-sensitive!

PHP has a total of eight data types which we use to construct our variables

 Integers − are whole numbers, without a decimal point, like 4195.

 Doubles − are floating-point numbers, like 3.14159 or 49.1.

 Booleans − have only two possible values either true or false.

 NULL − is a special type that only has one value: NULL.

 Strings − are sequences of characters, like 'Hello India', “Welcome”

 Arrays − are named and indexed collections of other values.

 Objects − are instances of programmer-defined classes, which can package up both other

kinds of values and functions that are specific to the class.

 Resources − are special variables that hold references to resources external to PHP (such as

database connections).

Setting up a variable in PHP


$var_name = value;

Example:
<?php
$txt = "Hello World!";
$number = 16;
?>

Strings in PHP
String variables are used for values that contain character strings.
Example:
<?php
$txt="Hello World";
echo $txt;
?>
There is only one string operator in PHP.

The Concatenation Operator


The concatenation operator (.) is used to put two string values together.To
concatenate two variables together, use the dot (.) operator:

Example:
<?php
$txt1="Hello World";
$txt2="1234";
echo $txt1 . " " . $txt2;
?>

String Function: strlen()


The strlen() function is used to find the length of a string.

Example:
Output:
<?php 12
Echo strlen("Hello world!");
?>

OPERATORS
Arithmetic Operators

OPERATOR DESCRIPTION EXAMPLE RESULT


+ Addition x=2 4
x+2
- Subtraction x=2 3
5-x
* Multiplication x=4 20
x*5
/ Division 15/5 3
5/2 2.5
% Modulus (division 5%2 1
remainder) 10%8 2
10%2 0
++ Increment x=5 x=6
x++
-- Decrement x=5 x=4
x--

Assignment Operators

OPERATOR DESCRIPTION EXAMPLE


== is equal to 5==8 returns false
!= is not equal 5!=8 returns true
> is greater than 5>8 returns false
< is less than 5<8 returns true
>= is greater than or equal 5>=8 returns false
to
<= is less than or equal to 5<=8 returns true

Logical Operators

OPERATOR DESCRIPTION EXAMPLE


&& and x=6
y=3 (x < 10 && y > 1)
returns true
|| Or x=6
y=3 (x==5 || y==5)
returns false
! not x=6
y=3 !(x==y) returns true

PHP If...Else Statements


If you want to execute some code if a condition is true and another code if a
condition is false, use the if....else statement.
Syntax
if (condition)
code to be executed if condition is true;
else
code to be executed if condition is false;

Example:
<html>
<body>
<?php $d=date("D");
if ($d=="Fri")
echo "Have a nice weekend!“;
else
echo "Have a nice day!";
?>
</body>
</html>

If more than one line should be executed if a condition is true/false, the


lines should be enclosed within curly braces:

<html>
<body>
<?php
$d=date("D");
if ($d=="Fri")
{
echo "Hello!<br />";
echo "Have a nice weekend!";
echo "See you on Monday!";
}
?>
</body>
</html>

The ElseIf Statement


If you want to execute some code if one of several conditions are true use the
elseif statement
Syntax
if (condition)
code to be executed if condition is true;
elseif (condition)
code to be executed if condition is true;
else
code to be executed if condition is false;

Example:
<html>
<body>
<?php
$d=date("D");
if ($d=="Fri")
echo "Have a nice weekend!";
elseif ($d=="Sun")
echo "Have a nice Sunday!";
else
echo "Have a nice day!";
?>
</body>
</html>

PHP Switch Statement


Use the switch statement to select one of many blocks of code to be executed.
Example:

<html> case 3:
<body> echo "Number 3";
<?php break;
switch ($x) default:
{ echo "No number between 1
case 1: and 3";
echo "Number 1"; }
break; ?>
case 2: </body>
This
echois"Number
how it2";works: </html>
First we have a single expression
break; n (most often a variable), that is evaluated once.
The value of the expression is then compared with the values for each case in the
structure. If there is a match, the block of code associated with that case is
executed. Use break to prevent the code from running into the next case
automatically. The default statement is used if no match is found.

PHP Loops
Often when you write code, you want the same block of code to run over and
over again in a row. Instead of adding several almost equal lines in a script we can
use loops to perform a task like this.
In PHP, we have the following looping statements:
 while - loops through a block of code while a specified condition is true
 do...while - loops through a block of code once, and then repeats the loop as
long as a specified condition is true
 for - loops through a block of code a specified number of times
 foreach - loops through a block of code for each element in an array

while loop
<html>
<body>
<?php
$i=1;
while($i<=5)
{
echo "The number is " . $i . "<br />";
$i++;
}
?>
</body>
</html>

do while loop
<html>
<body>
<?php
$i=1;
do
{
$i++;
echo "The number is " . $i . "<br />";
}
while ($i<=5);
?>
</body>
</html>

for loop
<html>
<body>
<?php
for ($i=1; $i<=5; $i++)
{
echo "The number is " . $i . "<br />";
}
?>
</body>
</html>

foreach loop
<html>
<body>
<?php
$x=array("one","two","three");
foreach ($x as $value)
{
echo $value . "<br />";
}
?>
</body>
</html>
What is an Array?

An array can store one or more values in a single variable name.

There are three different kinds of array:


1. Numeric array - An array with a numeric ID key
2. Associative array - An array where each ID key is associated with a value
3. Multidimensional array - An array containing one or more arrays

Numeric Array
A numeric array stores each element with a numeric ID key.

There are different ways to create a numeric array.

Example #1:
In this example the ID key is automatically assigned:
$names = array("Peter","Quagmire","Joe");

Example #2:
In this example we assign the ID key manually:
$names[0] = "Peter";
$names[1] = "Quagmire";
$names[2] = "Joe";

The ID keys can be used in a script:


<?php
$names[0] = "Peter";
$names[1] = "Quagmire";
$names[2] = "Joe";
echo $names[1] . " and " . $names[2] .
" are ". $names[0] . "'s neighbors";
?>

The code above will output:


Quagmire and Joe are Peter's neighbors

Associative Arrays
An associative array, each ID key is associated with a value.
When storing data about specific named values, a numerical array is not always
the best way to do it. With associative arrays we can use the values as keys and
assign values to them.

Example #1:
In this example we use an array to assign ages to the different persons:
$ages = array("Peter"=>32, "Quagmire"=>30, "Joe"=>34);

Example #2:
This example is the same as example 1, but shows a different way of creating the
array:
$ages['Peter'] = "32";
$ages['Quagmire'] = "30";
$ages['Joe'] = "34";

The ID keys can be used in a script: Output:


<?php
Peter is 32 years old.
$ages['Peter'] = "32"; $ages['Quagmire'] = "30"; $ages['Joe'] = "34";
echo "Peter is " . $ages['Peter'] . " years old.";
?>

FUNCTIONS IN PHP
A function is a block of code that can be executed whenever we need it.

Creating PHP functions:


 All functions start with the word "function()“
 Name the function - It should be possible to understand what the function
does by its name. The name can start with a letter or underscore (not a
number)
 Add a "{" - The function code starts after the opening curly brace
 Insert the function code
 Add a "}" - The function is finished by a closing curly brace

Example:
<html>
<body>
<?php
functionwriteMyName()
{
echo “Juan de la Cruz";
}
writeMyName();
?>
</body>
</html>

Now we will use the function in a PHP script:


<html>
<body>
<?php
functionwriteMyName()
{
echo “Juan de la Cruz";
}
echo "Hello world!<br />";
echo "My name is ";
writeMyName();
echo ".<br />That's right, ";
writeMyName();
echo " is my name.";
?>
</body>
</html>

PHP Functions – adding parameters


Our first function (writeMyName()) is a very simple function. It only writes
a static string.
To add more functionality to a function, we can add parameters. A parameter is
just like a variable.
You may have noticed the parentheses after the function name, like:
writeMyName(). The parameters are specified inside the parentheses.

Example #1- The following example will write different first names, but the
same last name:
<html>
<body>
<?php
Function writeMyName($fname)
{
echo $fname . " Cuneta.<br />";
}

echo "My name is ";


writeMyName(“Sharon");
echo "My name is ";
writeMyName(“Cristina");
echo "My name is ";
writeMyName(“Gabrielle");
?>
</body>
</html>

Example #2 - The following function has two parameters:


<html>
<body>
<?php
Function writeMyName($fname,$punctuation)
{
echo $fname . " Cuneta" . $punctuation . "<br />";
}
echo "My name is ";
writeMyName(“Sharon",".");
echo "My name is ";
writeMyName(“Cristina","!");
echo "My name is ";
writeMyName(“Gabrielle","...");
?>
</body>
</html>

PHP Functions – return values


<html>
<body>
<?php
function add($x,$y)
{ $total = $x + $y;
return $total;
}
echo "1 + 16 = " . add(1,16);
?>
</body>
</html>

PHP Form Handling


The most important thing to notice when dealing with HTML forms and PHP is that
any form element in an HTML page will automatically be available to your PHP
scripts.

The example below contains an HTML form with two input fields and a submit
button:
<html>
<body>
<form action="welcome.php" method="post">
Name: <input type="text" name="fname" />
Age: <input type="text" name="age" />
<input type="submit" />
</form>
</body>
</html>

When a user fills out the form above and click on the submit button, the
form data is sent to a PHP file, called "welcome.php":

"welcome.php" looks like this:


<html>
<body>
Welcome <?php echo $_POST["fname"]; ?>!<br />
You are <?php echo $_POST["age"]; ?> years old.
</body>
</html>

Output could be something like this:


Welcome John!
You are 28 years old.

Form Validation:
a. User input should be validated on the browser whenever possible (by client
scripts). Browser validation is faster and reduces the server load.
b. You should consider server validation if the user input will be inserted into a
database. A good way to validate a form on the server is to post the form to
itself, instead of jumping to a different page. The user will then get the error
messages on the same page as the form. This makes it easier to discover the
error.
PHP $_POST
In PHP, the predefined $_POST variable is used to collect values in a form with
method="post".

The $_POST Variable


The predefined $_POST variable is used to collect values from a form sent with
method="post".
Information sent from a form with the POST method is invisible to others and
has no limits on the amount of information to send.

Example:
<form action="welcome.php" method="post">
Name: <input type="text" name="fname" />
Age: <input type="text" name="age" />
<input type="submit" />
</form>

When the user clicks the "Submit" button, the URL will look like this:
http://www.w3schools.com/welcome.php

The "welcome.php" file can now use the $_POST variable to collect form data
(the names of the form fields will automatically be the keys in the $_POST
array):

Welcome <?php echo $_POST["fname"]; ?>!<br />


You are <?php echo $_POST["age"]; ?> years old.

When to use method="post"?


Information sent from a form with the POST method is invisible to others and
has no limits on the amount of information to send.

However, because the variables are not displayed in the URL, it is not possible to
bookmark the page.

PHP $_GET
In PHP, the predefined $_GET variable is used to collect values in a form with
method="get".

The $_GET Variable


The predefined $_GET variable is used to collect values in a form with
method="get"
Information sent from a form with the GET method is visible to everyone (it will
be displayed in the browser's address bar) and has limits on the amount of
information to send.

Example:
<form action="welcome.php" method="get">
Name: <input type="text" name="fname" />
Age: <input type="text" name="age" />
<input type="submit" />
</form>

When the user clicks the "Submit" button, the URL sent to the server could
look something like this:
http://www.w3schools.com/welcome.php?fname=Peter&age=37

The "welcome.php" file can now use the $_GET variable to collect form data
(the names of the form fields will automatically be the keys in the $_GET
array):
Welcome <?php echo $_GET["fname"]; ?>.<br />
You are <?php echo $_GET["age"]; ?> years old!

When to use method="get"?


When using method="get" in HTML forms, all variable names and values are
displayed in the URL.

Note:
a. This method should not be used when sending passwords or other sensitive
information!However, because the variables are displayed in the URL, it is
possible to bookmark the page. This can be useful in some cases.
b. The get method is not suitable for very large variable values. It should not be
used with values exceeding 2000 characters.

The PHP $_REQUEST Variable


The predefined $_REQUEST variable contains the contents of both $_GET,
$_POST, and $_COOKIE.
The $_REQUEST variable can be used to collect form data sent with both the
GET and POST methods.

Example:
Welcome <?php echo $_REQUEST["fname"]; ?>!<br />
You are <?php echo $_REQUEST["age"]; ?> years old.

You might also like