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

PHP-notes

The document provides an overview of server-side scripting using PHP, detailing its features, limitations, and basic syntax. It covers PHP variables, data types, control structures like loops and conditionals, and includes examples of PHP code for various operations. Additionally, it discusses PHP functions and provides workout program examples demonstrating practical applications of PHP coding.

Uploaded by

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

PHP-notes

The document provides an overview of server-side scripting using PHP, detailing its features, limitations, and basic syntax. It covers PHP variables, data types, control structures like loops and conditionals, and includes examples of PHP code for various operations. Additionally, it discusses PHP functions and provides workout program examples demonstrating practical applications of PHP coding.

Uploaded by

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

Server-Side Scripting Using PHP

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 if …else statement


Program to check whether a person can vote or not.
<html><body>
<?php
$age = 20;
if($age >= 18)
{
echo "You are eligible to cast vote";
}
else
{
echo "You are not eligible to caste vote";
}
?>
</body></html>
The for-loop
PHP code to display the natural numbers from 1 to 10.
<html>
<head>
<title>show</title>
</head>
<body>
<?php
for($a=1;$a<=10;$a++)
{
echo"$a ";
}
?>
</body>
</html>

The while loop


Program to print alphabets from A to I
<html>
<body>
<?php
$i = 'A';
while ($i<='I')
{
echo$i;
echo"\t";
$i++;
}
?>
</body>
</html>
The foreach loop statement:
This loop is accepting an array as variable and considering of executing code till the last element of the array.
Syntax
foreach ($arr_var as $val)
{[PHP code block]}
Program to multiply array numbers using foreach loop
<html>
<body>
<?php
$arr = array(1,2,3,4,5,6,7,8,9,10);
foreach ($arr as $i){
echo$i . "x2=" . 2*$i ."
<br>";
}
?></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>

Some workout program examples in PHP coding.


1. PHP code to input two numbers calculate and display their sum.
<html><body>
<formmethod="post">
Enter First Number:
<input type="number"name="number1"/><br><br>
Enter Second Number:
<input type="number"name="number2"/><br><br>
<input type="submit"name="submit"value="Add">
</form>
<?php
if(isset($_POST['submit']))
{
$number1 = $_POST['number1'];
$number2 = $_POST['number2'];
$sum = $number1+$number2;
echo "The sum of $number1 and $number2 is: ".$sum;
}
?>
</body>
</html>

3. PHP code to calculate the factorial of given number.


<html><head>
<title>Factorial Program using in PHP</title>
</head><body>
<form method="post">
Enter the Number:<br>
<input type="number"name="number">
<input type="submit"name="submit"value="Submit"/>
</form>
<?php
if($_POST){
$fact = 1;
//getting value from input text box 'number'
$number = $_POST ['number'];
echo "Factorial of $number:<br><br>";
//start loop
for ($i = 1; $i<= $number; $i++){
$fact = $fact * $i;
}
echo $fact . "<br>";
}
?></body></html>
4. PHP code to check entered number is even or odd using form.
<html><body>
<form method="post">
Enter a number:
<input type="number"name="number">
<input type="submit"value="Submit">
</form></body>
<?php
if($_POST)
{
$number = $_POST['number'];
if(($number % 2) == 0){
echo "$number is an Even number";
}else
{
echo "$number is Odd number";
}}
?></html>
5. PHP code to calculate and display sum of each digit of number 234.
<html><body>
<?php
$num = 234;
$sum=0; $rem=0;
for ($i =0; $i<=strlen($num);$i++)
{
$rem=$num%10;
$sum = $sum + $rem;
$num=$num/10;
}
echo"Sum of digits 234 is $sum";
?>
</body> </html>

You might also like