PHP Workshop Kit
PHP Workshop Kit
The company has remarkable establishment in the field of software development with
more than 200 clients (Domestic & International), with major clients like Medicounsel
(US), Gas Authority of India Limited, Indian Oil Corporation Limited, Sahara India,
Commander Works Engineer (CWE) & Garrison Engineers (Ministry of Defense).
"Softpro Learning Center", the training division of the company was established in 2008
with the clear vision to reduce the Technology Gap preva
prevailing
iling between IT students and
IT professionals. We have come a long way since the start; the company is an ISO 9001:
2000 certified company. Softpro Learning Center has established itself as one of the
most promising center for learning across UP and nearb
nearby states.
SLC services include imparting Summer Training to B.Tech students, Industrial Training
to MCA/BCA students and Apprenticeship Program to pass out students of
B.Tech/BCA/MCA. In the year 2015, we had a crowd of approx. 2000 students who did
their summer training from Softpro making us the largest learning centers of North
India. Moreover 60% of the total trainees of ours are placed in big brands like TCS and
Wipro.
Our Mission:
Since its inception, Development and Educational Wings of Softpro India Ind Computer
Technologies (P) Ltd. with its goal oriented mission is perpetually achieving success.
Features of PHP
It is the most popular and frequently used world wide scripting language, the main reason of popularity
is; It is open source and very simple.
Simple
It is very simple and easy to use, compare
compared to other scripting languages and is widely used all over
the world.
Interpreted
It is an interpreted language i.e. there is no need for compilation.
Faster
It is faster than other scripting language
languages e.g. asp and jsp.
Open Source
Open source means you need not have to pay for using php, you can freely download and use.
Case Sensitive
PHP is case
ase sensitive scripting language at the time of variable declaration. In PHP, all keywords (e.g.
if, else, while, echo, etc.), classes, functions, and user
user-defined
defined functions are NOT case-sensitive.
case
Uses of PHP
• You can use PHP to find today's date, and then build a calendar for the month.
• If you host banner advertisements on your website, you can use PHP to rotate them randomly.
• You can use PHP to create a special area of your website for members.
• Using php you can create login page for your user.
Using php you can add, delete and modify elements
ements within your database through PHP. Access
cookies variables and set cookies.
• Using PHP, you can restrict users to access some pages of your website.
• PHP performs system functions, i.e. from files on a system it can create, open, read, write, and
close them.
• It can handle forms, i.e. gather data from files, save data to a file.
Prerequisites
To install PHP, we will suggest you to install AMP (Apache, MySQL, PHP) software stack. It is available
for all operating systems. There are many AMP options available in the market that are given below:
If you are on Windows and don't want Perl and other features o
off XAMPP, you should go for WAMP.
In a similar way, you may use LAMP for Linux and MAMP for Macintosh.
PHP Syntax
<?php
// PHP code goes here
?>
PHP files save with .php extension and it contain some HTML and PHP code.
PHP Syntax
<!DOCTYPE html>
<html>
<body>
<?php
echo "Hello World!";
?>
</body>
</html>
Output
Hello World!
Variable in PHP
Variable is a container that contains a value, which may get change during the execution of the
program.
$variablename=value;
<?php
$str="Hello world!";
$a=5;
$b=10.5;
echo "String is: $str <br/>";
echo "Integer is: $x <br/>";
echo "Float is: $y <br/>";
?>
Output
Integer is: 5
Variable $str will hold the string value Hello world!,, variable $x will hold the interger value 5 and
variable $y will hold float value 10.5
10.5.
• A variable starts with the $ sign, followed by the name of the variable
• $_GET
• $_POST
• $_REQUEST
• $_SESSION
• $_COOKIES
Constant in PHP
Constants are name or identifier that can't be changed during the execution of the script. In php
constants are define in two ways;
In php decalare constants follow same rule variable declaration. Constant start with letter or
underscore only.
Syntax
• case-insensitive:
insensitive: Specifies whether the constant name should be case
case-insensitive.
insensitive. Default is false
<?php
define("MSG","Hello world!");
echo MSG;
?>
<?php
define("MSG","Hello world!", true
true);
echo msg;
?>
Output
Hello world!
Define constant
nstant using cons keyword in PHP
The const keyword defines constants at compile time. It is a language construct not a function. It is bit
faster than define(). It is always case sensitive.
<?php
cons MSG="Hello world!";
echo MSG;
?>
Output
Hello world!
Datatype in PHP
PHP data types are used to hold different types of data or values. PHP supports the following data
types:
• String
• Integer
Comments in PHP
Comments in any programming language is used to discribe code and make simple to understand
other programmer and it is ignore by compiler or interpreter.
PHP supports single line and multi line comments. PHP comments are similar to C/C++ and
an Perl style
(Unix shell style) comments.
Syntax
<?php
?>
Output
Welcome to PHP single line comments
In PHP, we can comments multiple lines also. To do so, we need to enclose all lines within /* */. Let's
see a simple example of PHP multiple line comment.
Syntax
<?php
/*
Anything placed
within comment
will not be displayed
on the browser;
*/
echo "Welcome to PHP multi line comment"
comment";
?>
Output
Welcome to PHP multi line comment
In PHP there are four way to get your output is; echo, print, print_r(), printf().
<?php
echo "<h2>My First PHP Code</h2>"
Code</h2>";
?>
Output
My First PHP Code
<?php
$str="Hello php"
echo "<h2>My Message: $str</h2>"
$str</h2>";
?>
Output
My Message: Hello PHP
echo has no return value while print has a return value of 1 so it can be used in expressions.
Operators in PHP
Operator is a symbol which is used to perform operations on operands. Here you can see very simple
example of operator
Example
$num=4+5;
In the above example, + is the binary + operator, 4 and 5 are operands and $num is variable.
• Arithmetic Operators
• Comparison Operators
• Bitwise Operators
• Logical Operators
• String Operators
• Incrementing/Decrementing Operators
• Array Operators
• Assignment Operators
The for loop is used when you know in advance how many times the script should run. In php for loops
execute a block of code specified number of times.
Syntax
<?php
?>
Output
0 1 2 3 4 5 6 7 8 9 10
The while loop is used when you don't know how many times the script should run. while loops
execute a block of code while the specified condition is true same like for loop.
while(condition)
{
//code to be executed
}
<?php
$n=0;
while($n<=10)
{
echo "$n<br/>";
$n++;
}
?>
Output
0 1 2 3 4 5 6 7 8 9 10
do..while loop is used where you need to execute code at least once. The do...while loop will always
execute the block of code once, it will then check the condition, and repeat the loop while the
specified condition is true.
Syntax
do
{
//code to be executed
}
while(condition);
<?php
$x = 0;
do
{
echo "$x <br>";
$x++;
} while ($x <= 10);
?>
Output
0 1 2 3 4 5 6 7 8 9 10
if else in PHP
In php if else statement is used to test condition. If conditon is true execute the code other wise
control goes outside.
PHP If Statement
Syntax
if(condition)
{
//code to be executed
}
<?php
$age=20;
if($age<18)
{
echo "$You are Adult";
}
?>
Output
You are Adult
PHP if-else
else statement is executed whether condition is true or false.
Syntax
if(condition)
{
//code to be executed if true
}
else
{
//code to be executed if false
}
<?php
$num=10;
if($num%2==0)
{
echo "$num is even number";
}
else
{
?>
Output
10 is even number
Syntax
jump statement;
break;
<?php
for($i=1;$i<=10;$i++)
{
echo "$i <br/>";
if($i==5)
{
break;
}
}
?>
<?php
for($i=1;$i<=3;$i++)
{
for($j=1;$j<=3;$j++)
{
echo "$i $j<br/>";
if($i==2 && $j==2)
{
break;
}
}
}
?>
Output
1112132122313233
<?php
$num=5;
switch($num){
case 1:
echo("Monday");
break;
case 2:
echo("Tuesday");
break;
case 3:
echo("Wednesday");
break;
case 4:
echo("Thursday");
break;
case 5:
echo("Friday");
break;
case 6:
echo("Saturday");
break;
case 7:
echo("Sunday");
break;
?>
Output
Friday
PHP have lots of predefined function which is used to perform operation with string some functions
are
strlen()
strrev()
strpos()
str_word_count()
str_replace()
strtolower()
strtoupper()
ucwords()
ucfirst()
lcfirst()
Array in PHP
Array :-An
An array stores multiple values in one single variable:
PHP have lots of predefined function which is used to perform operation with array some functions
are
array()
array_filter()
array_key_exists()
array_fill()
array_slice()
array_sum() etc
mysqli_connect() function is used to connect php code with MySQL database. It returns resource if
connection is established otherwise null.
Syntax
PHP mysqli_close()
mysqli_close() function is used to disconnect php code with MySQL database. It returns true if
connection is closed otherwise false.
Syntax
bool mysqli_close(resource
resource $resource_link
$resource_link)
<?php
$servername = "localhost";
$username = "username";
$password = "password";
// Create connection
$conn = mysqli_connect($servername
$servername, $username, $password);
// Check connection
if (!$conn)
{
die("Connection failed: " . mysqli_connect_error
mysqli_connect_error());
}
echo "Connected successfully";
mysqli_close($conn);
?>
Using now() function you can get current data and time and store it into database but first choose
column name date and time because now() now work with date datatype.
<?php
$mydate=getdate(date("U"));
echo "$mydate[weekday], $mydate[month] $mydate[mday], $mydate[year]"
$mydate[year]";
?>
Live Demo
Thursday, February 2, 2017
<?php
$sql = "insert into user(f_name, l_name, date)
VALUES('$f_name', '$l_name', now())"
now())";
$result = mysql_query($sql);
?>
To transfer data from one web page to another webpage we need Html form and using
usin action="" we
transfer form data on another page.
Using $_POST['field_name'] we receive form data on another page in PHP if method is post in case of
get method use $_GET['field_name'];.
index.html
Html page
display.php
Receive form data on other page
<?php
$first_name = $_POST['fname'];
$last_name = $_POST['lname'];
echo $first_name;
echo $last_name;
?>
When you fill html form all data will be transfer from index.php to display.php
What is PHP ?
PHP stands for Hypertext Preprocessor It is open source server side scripting language. It support
many database for example: MySQL, Oracle, Sybase, Solid, PostgreSQL, generic ODBC etc.
Print return value but echo not return any value .Echo is faster than print because it does not return
any value.
Require and include both are used to include a file, but if file is not found iinclude
nclude sends warning
whereas require sends Fatal error.
• WordPress
• Drupal
• Joomla
• Magento
Count() function is used to count total elements in the array, or something an object.
The main difference between session and cookies is that cookies are stored on the user's computer in
the text file format while sessions are stored on the server side.
You
ou can manually set an expiry for a cookie, while session only remains active as long as browser is
open.
Syntax
Syntax
Syntax
bool mail($to,$subject,$message
$message,$header);
Using JavaScript submit() function you can submit the form without explicitly clicking any submit
button.
Syntax
$text is a variable with a fixed name. $$text is a variable whose name is stored in $text .
If $text contains "var", $$text is the same as $var.
Using exit() function you can stop the execution of PHP script.
• Symfony
• CodeIgniter
• Yii 2
• Zend Framework
Using mysql_create_db("Database Name") you can create a database using PHP and MySQL.
• Procedural
• Warnings: These types of error are more serious errors but they do not result in script
termination. By default, these errors are displayed to the user.
• Fatal Errors: These types of error are the most critical errors. These errors may be a cause of
immediate termination of script.
Encryption
on functions in php are; CRYPT() and MD5()
<?php
Session_register($ur_session_var
$ur_session_var);
?>
What is a session ?
PHP Engine creates a logical object to preserve data across subsequent HTTP requests, which is known
as session.
Sessions generally store temporary data to allow multiple PHP pages to offer a complete functional
transaction for the same user.
Some Assignment
ID Task – 1
1.1 Write
ite a program in php to find greatest no. among three number.
1.3 Write
te a program in php to check the no. is odd or even.
ID Task - 2
2.3 Write a program in php to accept an integer and print it in reverse order.
Input= 123
Output=321
2.4 Write a program in php to enter a character in a variable and check that the character is
vowel or consonant using switch
witch case.
2.5 Write a program in php to calculate sum of all no. like 5236 5+2+3+6=16
ID Task -3
3.1 Write a program to enter n no. in an array and calculate the addition and average of the array
and display the calculation.
3.2 Develop a web page using php to take username as input and display username in Uppercase
and Lowercase Letters.
3.3 Develop a web page using php to check whether two strings are equal or not.
3.4 Develop a web page using php to copy one string to another.
3.6 Develop a web page using php to calculate highest no. of an array.
3.7 Develop a web page using php to calculate lowest no. of an array.
3.8 Develop a web page using php to check the given sting is palindrome or not.
Thanks,
Rohit Kumar