KEMBAR78
Assignment 1 | PDF | Php | World Wide Web
0% found this document useful (0 votes)
29 views13 pages

Assignment 1

The document is an assignment from Shri Vaishnav Institute of Computer Application covering various aspects of PHP, including its characteristics, advantages, disadvantages, form handling, validation, and functions. It explains PHP as a server-side scripting language, its integration with databases, and its usage in web development. Additionally, it discusses form validation techniques and the importance of functions in PHP programming.

Uploaded by

Zeeshan Khan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views13 pages

Assignment 1

The document is an assignment from Shri Vaishnav Institute of Computer Application covering various aspects of PHP, including its characteristics, advantages, disadvantages, form handling, validation, and functions. It explains PHP as a server-side scripting language, its integration with databases, and its usage in web development. Additionally, it discusses form validation techniques and the importance of functions in PHP programming.

Uploaded by

Zeeshan Khan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

Shri Vaishnav Institute of Computer Application

Assignment – l
Semester – IV
Section - C

Submitted by : Submitted To :
Mohammad Zeeshan Khan Prof. Priyasi Jain
Enrollment no:
2204MCA0013238
INDEX
S.no Subject Page No

1. Question 1 1-2

2. Question 2 3-4

3. Question 3 5

4. Question 4 6-8

5. Question 5 9-10
Q1. What is PHP , explain its characteristics and features.

The term PHP is an acronym for – Hypertext Preprocessor. PHP is a server-side scripting
language designed specifically for web development. It is open-source which means it is free
to download and use. It is very simple to learn and use. The file extension of PHP is “.PHP”.
PHP was introduced by Rasmus Lerdorf in the first version and participated in the later
versions. It is an interpreted language and it does not require a compiler.

 Characteristics of PHP
PHP code is executed in the server.
It can be integrated with many databases such as Oracle, Microsoft SQL Server,
MySQL, PostgreSQL, Sybase, and Informix.
It is powerful to hold a content management system like WordPress and can be used to
control user access.
It supports main protocols like HTTP Basic, HTTP Digest, IMAP, FTP, and others.
Websites like www.facebook.com and www.yahoo.com are also built on PHP.
One of the main reasons behind this is that PHP can be easily embedded in HTML
files and HTML codes can also be written in a PHP file.
The thing that differentiates PHP from the client-side language like HTML is, that
PHP codes are executed on the server whereas HTML codes are directly rendered on
the browser. PHP codes are first executed on the server and then the result is returned
to the browser.
The only information that the client or browser knows is the result returned after
executing the PHP script on the server and not the actual PHP codes present in the
PHP file. Also, PHP files can support other client-side scripting languages like CSS
and JavaScript.

 Syntax
<?php
PHP code goes here
?>

 Features of PHP

1. Dynamic Typing: PHP is dynamically typed, meaning you don’t need to declare
the data type of a variable explicitly.
2. Cross-Platform: PHP runs on various platforms, making it compatible with
different operating systems.
3. Database Integration: PHP provides built-in support for interacting with databases,
such as MySQL, PostgreSQL, and others.
4. Server-Side Scripting: PHP scripts are executed on the server, generating HTML
that is sent to the client’s browser.
Page | 1
 Origin and History

 PHP was created by Rasmus Lerdorf in 1994 and initially stood for “Personal
Home Page.” Over the years, it evolved into “PHP: Hypertext Preprocessor.”
 PHP is open-source and has a large community of developers contributing to its
development.

 Purpose of using PHP

 PHP is primarily used for server-side web development. It enables the creation of
dynamic web pages by embedding PHP code within HTML.
 PHP can perform various tasks, including handling form data, generating dynamic
page content, managing databases, and interacting with servers.

Page | 2
Q2. What are advantages and disadvantages of PHP.

 Advantages of PHP

 Open Source: PHP is an open-source language, making it freely available for use
and distribution. This encourages a large community of developers, contributing to
its growth and improvement.
 Easy to Learn: PHP syntax is similar to C and other programming languages,
making it relatively easy for developers to learn, especially for those with a
background in programming.
 Web Integration: PHP is designed specifically for web development and is
embedded within HTML. It seamlessly integrates with various web technologies,
facilitating the creation of dynamic and interactive web pages.
 Database Support: PHP has excellent support for various databases, including
MySQL, PostgreSQL, SQLite, and more. This makes it easy to connect and
interact with databases, a crucial aspect of many web applications.
 Cross-Platform Compatibility: PHP is platform-independent and runs on
various operating systems, including Windows, Linux, macOS, and others. This
ensures compatibility across different environments.
 Large Community and Documentation: PHP has a vast and active community
of developers. The abundance of online resources, tutorials, and documentation
makes it easier for developers to find solutions and seek help when needed.
 Frameworks and CMS: There are popular PHP frameworks like Laravel,
Symfony, and CodeIgniter, which provide pre-built modules and features, aiding in
rapid development. Additionally, PHP supports widely used content management
systems (CMS) like WordPress and Joomla.
 Server-Side Scripting: PHP scripts are executed on the server, reducing the load
on the client’s side. This server-side scripting capability is crucial for generating
dynamic content and performing server-related tasks.
 Community Support: The PHP community actively contributes to the
language’s development, ensuring regular updates, security patches, and
improvements.
 Disadvantages of PHP

 Inconsistency: PHP has been criticized for inconsistencies in function names and
parameter orders. This can lead to confusion for developers, especially when
working with a mix of older and newer functions.
 Security Concerns: If not handled properly, PHP code may be susceptible to
security vulnerabilities, such as SQL injection and cross-site scripting (XSS).

Page | 3
 Developers need to be cautious and follow best practices to secure PHP
applications.

 Performance: While PHP performs well for many web applications, it may not
be as fast as some compiled languages like C or Java. However, advancements and
optimizations in recent versions have improved performance.
 Lack of Modern Features: Compared to newer languages, PHP may lack some
modern language features. However, recent versions of PHP have introduced
improvements and features to address this concern.
 Scalability Challenges: PHP can face challenges when it comes to scaling large
and complex applications. Developers may need to adopt additional tools or
frameworks to address scalability issues.
 Not Suitable for Large-Scale Applications: While PHP is suitable for small to
medium-sized projects, it might not be the best choice for extremely large and
complex applications where more structured languages might be preferred.
 Limited Object-Oriented Programming (OOP) Support: Although PHP
supports OOP, its implementation has been criticized for not being as robust as in
some other languages. However, recent versions have introduced improvements to
enhance OOP capabilities.

Page | 4
Q3. Introduction to form in PHP programming.

One of the most powerful features of PHP is the way it handles HTML forms. The basic
concept that is important to understand is that any form element will automatically be
available to your PHP scripts. Please read the manual section on Variables from external
sources for more information and examples on using forms with PHP. Here is an example
HTML form:

Example -1 A simple HTML form

<form action="action.php" method="post">


<label for="name">Your name:</label>
<input name="name" id="name" type="text">

<label for="age">Your age:</label>


<input name="age" id="age" type="number">

<button type="submit">Submit</button>
</form>

There is nothing special about this form. It is a straight HTML form with no special tags of
any kind. When the user fills in this form and hits the submit button, the action.php page is
called. In this file you would write something like this:
Example -2 Printing data from our form
Hi <?php echo htmlspecialchars($_POST['name']); ?>.
You are <?php echo (int)$_POST['age']; ?> years old.

A sample output of this script may be:


Hi Joe. You are 22 years old.
Apart from the htmlspecialchars() and (int) parts, it should be obvious what this
does. htmlspecialchars() makes sure any characters that are special in html are properly
encoded so people can't inject HTML tags or JavaScript into your page. For the age field,
since we know it is a number, we can just convert it to an int which will automatically get rid
of any stray characters. You can also have PHP do this for you automatically by using
the filter extension. The $_POST['name'] and $_POST['age'] variables are automatically set
for you by PHP. Earlier we used the $_SERVER super-global; above we just introduced
the $_POST super-global which contains all POST data. Notice how the method of our form
is POST. If we used the method GET then our form information would live in
the $_GET super-global instead. You may also use the $_REQUEST super-global, if you do
not care about the source of your request data. It contains the merged information of GET,
POST and COOKIE data.

You can also deal with XForms input in PHP, although you will find yourself comfortable
with the well supported HTML forms for quite some time. While working with XForms is
not for beginners, you might be interested in them. We also have a short introduction to
handling data received from XForms in our features section.
Page | 5
Q4. Introduction to form validation in PHP programming.

 Form Validation in PHP


An HTML form contains various input fields such as text box, checkbox, radio
buttons, submit button, and checklist, etc. These input fields need to be validated,
which ensures that the user has entered information in all the required fields and also
validates that the information provided by the user is valid and correct.
There is no guarantee that the information provided by the user is always
correct. PHP validates the data at the server-side, which is submitted by HTML form.
You need to validate a few things:
 Empty String
 Validate String
 Validate Numbers
 Validate Email
 Validate URL
 Input length
 Empty String
The code below checks that the field is not empty. If the user leaves the required field
empty, it will show an error message. Put these lines of code to validate the required
field.

if (emptyempty ($_POST["name"])) {
$errMsg = "Error! You didn't enter the Name.";
echo $errMsg;
} else {
$name = $_POST["name"];
}

 Validate String
The code below checks that the field will contain only alphabets and whitespace, for
example - name. If the name field does not receive valid input from the user, then it
will show an error message:

$name = $_POST ["Name"];


if (!preg_match ("/^[a-zA-z]*$/", $name) ) {
$ErrMsg = "Only alphabets and whitespace are allowed.";
echo $ErrMsg;
} else {
echo $name;
}

 Validate Number
The below code validates that the field will only contain a numeric value. For example
Page | 6
- Mobile no. If the Mobile no field does not receive numeric data from the user, the
code will display an error message:

$mobileno = $_POST ["Mobile_no"];


if (!preg_match ("/^[0-9]*$/", $mobileno) ){
$ErrMsg = "Only numeric value is allowed.";
echo $ErrMsg;
} else {
echo $mobileno;
}

 Validate Email
A valid email must contain @ and . symbols. PHP provides various methods to
validate the email address. Here, we will use regular expressions to validate the email
address.
The below code validates the email address provided by the user through HTML form.
If the field does not contain a valid email address, then the code will display an error
message:

$email = $_POST ["Email"];


$pattern = "^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$^";
if (!preg_match ($pattern, $email) ){
$ErrMsg = "Email is not valid.";
echo $ErrMsg;
} else {
echo "Your valid email address is: " .$email;
}

 Input Length Validation


The input length validation restricts the user to provide the value between the specified
range, for Example - Mobile Number. A valid mobile number must have 10 digits.
The given code will help you to apply the length validation on user input:

$mobileno = strlen ($_POST ["Mobile"]);


$length = strlen ($mobileno);

if ( $length < 10 && $length > 10) {


$ErrMsg = "Mobile must have 10 digits.";
echo $ErrMsg;
} else {
echo "Your Mobile number is: " .$mobileno;
}

 Validate URL
The below code validates the URL of website provided by the user via HTML form. If

Page | 7
the field does not contain a valid URL, the code will display an error message, i.e.,
"URL is not valid".

$websiteURL = $_POST["website"];
if (!preg_match("/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-
9+&@#\/%=~_|]/i",$website)) {
$websiteErr = "URL is not valid";
echo $websiteErr;
} else {
echo "Website URL is: " .$websiteURL;
}

 Button Click Validate


The below code validates that the user click on submit button and send the form data
to the server one of the following method - get or post.

if (isset ($_POST['submit']) {
echo "Submit button is clicked.";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
echo "Data is sent using POST method ";
}
} else {
echo "Data is not submitted";
}

Page | 8
Q5. Introduction to functions in PHP programming.
 PHP Functions
PHP function is a piece of code that can be reused many times. It can take input as
argument list and return value. There are thousands of built-in functions in PHP.
In PHP, we can define Conditional function, Function within Function and Recursive
function also.

 Advantage of PHP Functions

 Code Reusability: PHP functions are defined only once and can be invoked many
times, like in other programming languages.
 Less Code: It saves a lot of code because you don't need to write the logic many
times. By the use of function, you can write the logic only once and reuse it.
 Easy to understand: PHP functions separate the programming logic. So it is easier
to understand the flow of the application because every logic is divided in the form
of functions.
 PHP User-defined Functions
We can declare and call user-defined functions easily. Let's see the syntax to declare
user-defined functions.

Syntax

function functionname(){
//code to be executed
}

PHP Functions Example


File: function1.php

<?php
function sayHello(){
echo "Hello PHP Function";
}
sayHello();//calling function
?>

Output:

Hello PHP Function

 PHP Function Arguments


We can pass the information in PHP function through arguments which is separated by
comma.
Page | 9
PHP supports Call by Value (default), Call by Reference, Default argument
values and Variable-length argument list.
Let's see the example to pass single argument in PHP function.
File: functionarg.php

<?php
function sayHello($name){
echo "Hello $name<br/>";
}
sayHello("Sonoo");
sayHello("Vimal");
sayHello("John");
?>

Output:

Hello Sonoo
Hello Vimal
Hello John

Let's see the example to pass two argument in PHP function.


File: functionarg2.php

<?php
function sayHello($name,$age){
echo "Hello $name, you are $age years old<br/>";
}
sayHello("Sonoo",27);
sayHello("Vimal",29);
sayHello("John",23);
?>

Output:

Hello Sonoo, you are 27 years old


Hello Vimal, you are 29 years old
Hello John, you are 23 years old

 PHP Call By Reference

Value passed to the function doesn't modify the actual value by default (call by value).
But we can do so by passing value as a reference.
By default, value passed to the function is call by value. To pass value as a reference,
you need to use ampersand (&) symbol before the argument name.
Let's see a simple example of call by reference in PHP.
File: functionref.php
Page | 10
<?php
function adder(&$str2)
{
$str2 .= 'Call By Reference';
}
$str = 'Hello ';
adder($str);
echo $str;
?>

Output:

Hello Call By Reference

 PHP Function: Default Argument Value

We can specify a default argument value in function. While calling PHP function if
you don't specify any argument, it will take the default argument. Let's see a simple
example of using default argument value in PHP function.
File: functiondefaultarg.php
<?php
function sayHello($name="Sonoo"){
echo "Hello $name<br/>";
}
sayHello("Rajesh");
sayHello();//passing no value
sayHello("John");
?>

Output:
Hello Rajesh
Hello Sonoo
Hello John

 PHP Function: Returning Value


Let's see an example of PHP function that returns value.
File: functiondefaultarg.php
<?php
function cube($n){
return $n*$n*$n;
}
echo "Cube of 3 is: ".cube(3);
?>
Output:
Cube of 3 is: 27
Page | 11

You might also like