PHP is a widely used scripting language for web development. It is installed on web servers and used to handle server-side operations like connecting to databases, generating dynamic page content, and interacting with forms. Some key points about PHP include:
- It is free and open source, allowing developers to easily build both static and dynamic web pages.
- Popular websites like Facebook and Wikipedia use PHP for its flexibility and wide server support.
- PHP code is embedded into HTML and executed on the server before the page is sent to the browser.
2. What is PHP?
• PHP stands for Hypertext Pre-Processor. PHP is a scripting language used to
develop static and dynamic webpages and web applications. Here are a few
important things you must know about PHP:
• PHP is one of the most widely used server-side scripting language for web
development. Popular websites like Facebook, Yahoo, Wikipedia etc., are
developed using PHP.
1.PHP is an Interpreted language; hence it doesn't need a compiler.
2.To run and execute PHP code, we need a Web server on which PHP must be
installed.
3.PHP is a server-side scripting language, which means that PHP is executed on the
server and the result is sent to the browser in plain HTML.
4.PHP is open source and free.
3. Features of PHP
1.PHP is open source and free, hence you can freely download, install and start developing
using it.
2.PHP has a very simple and easy to understand syntax, hence the learning curve is
smaller as compared to other scripting languages like JSP, ASP etc.
3.PHP is cross platform; hence you can easily develop and move/deploy your PHP
code/project to almost all the major operating systems like Windows, Linux, Mac OSX
etc.
4.All the popular web hosting services support PHP. Also, the web hosting plans for PHP
are generally the amongst the cheapest plans because of its popularity.
5.Popular Content Management Systems like Joomla, Drupal etc. are developed in PHP.
6.With PHP, you can create static and dynamic webpages, perform file handling operations,
send emails, access and modify browser cookies, and almost everything else that you
might want to implement in your web project.
7.PHP is fast as compared to other scripting languages like JSP and ASP.
8.PHP has in-built support for MySQL, which is one of the most widely used Database
management system.
4. Uses of PHP
1.It can be used to create Web applications like Social Networks (Facebook,
Digg), Blogs (WordPress, Joomla), eCommerce websites (OpenCart, Magento
etc.) etc.
2.Command Line Scripting. You can write PHP scripts to perform different
operations on any machine, all you need is a PHP parser for this.
3.Create Facebook applications and easily integrate Facebook plugins in your
website, using Facebook's PHP SDK.
4.Sending Emails or building email applications because PHP provides with a
robust email sending function.
5.WordPress is one of the most used blogging (CMS) platforms in the World, and if
you know PHP, you can try a hand in WordPress plugin development.
5. History of PHP
• Originally (it is believed) the letters stood for Personal Home Page. The first work
was done by Rasmus Lerdorf and dates back to about 1994. Lerdorf originally
used it for his personal page – specifically to track visitors. Soon, a lot of
additional functions were added. It took a few years, though, for it to become a
language and not just a set of tools.
• The code was released in the mid-1990s. Israel’s Andi Gutmans and Zeev Suraski
did a major overhaul in 1997 with the goal of using PHP to run an eCommerce
site. Their version, termed PHP 3.0, had more of the features we expect and see
today.
• The same duo later created Version 4.0. 2004 saw another major revision, PHP
5.0. Although there has not yet been a 6.0, there have been significant
improvements since 5.0, including the removal of several things that had caused
instability or potential security breaches.
6. Basic PHP syntax
• A PHP script can be placed anywhere in the document.
• A PHP script :
• starts with <?php and ends with ?>:
• The default file extension for PHP files is ".php".
• A PHP file normally contains HTML tags, and some PHP scripting code.
• PHP statement ends with semi colon (;).
• In PHP, keywords (e.g. if, else, while, echo, etc.), classes, functions, and user-
defined functions are not case-sensitive.
• In PHP variables are case sensitive.
<?php
// PHP code goes here
?>
7. Why use PHP along with HTML
• PHP allows dynamic web page generation, which is not possible by using just
HTML. Thus by using PHP script along with HTML in a PHP file, the web
developers can generate dynamic web pages. The use of PHP also allows access to
various databases.
• PHP is used for server-side programming which will interact with databases to
retrieve information, storing, email sending, and provides content to HTML pages
to display on the screen.
9. PHP Form Handling
• The PHP superglobals $_GET and $_POST are used to collect form-data.
• <html>
<body>
<form action="welcome.php" method="post">
Name: <input type="text" name="name"><br>
E-mail: <input type="text" name="email"><br>
<input type="submit">
</form>
</body>
</html>
• When the user fills out the form above and clicks the submit button, the form data
is sent for processing to a PHP file named "welcome.php". The form data is sent
with the HTTP POST method.
• $_GET retrieves variables from the querystring, or your URL.
• $_POST retrieves variables from a POST method, such as (generally) forms.
10. • $_POST is an associative array of variables passed to the current script via the
HTTP POST method when using application/x-www-form-urlencoded or
multipart/form-data as the HTTP Content-Type in the request. You can use when
you are sending large data to server or if you have sensitive information like
passwords, credit card details etc.
• $_GET is an associative array of variables passed to the current script via the
URL parameters. you can use when there is small amount of data, it is mostly used
in pagination, page number is shown in the url and you can easily get the page
number from URL using $_GET.
11. Embedding PHP and HTML
• Step 1: Firstly, we have to type the Html code in any text editor or open the
existing Html file in the text editor in which we want to use the PHP.
• Step 2: Now, we have to place the cursor in any tag of the <body> tag where we
want to add the code of PHP. And, then we have to type the start and end tag of
PHP.
• Step 3: After then, we have to type the code of PHP between the tags of PHP.
• Step 4: When we successfully add the PHP code, then we have to save the Html
file and then run the file in the browser.
12. How PHP code is parsed
• So you have a file, and in that file you have some HTML and some PHP code.
This is how it all works, assuming a PHP document with an extension of .php.
• The web browser requests a document with a .php extension.
• The web server says, "Hey! Someone wants a PHP file, which means this is a
file that needs to be parsed," and sends the request on to the PHP parser.
• The PHP parser finds the requested file and scans it for PHP code.
• When the PHP parser finds PHP code, it executes that code and places the
resulting output (if any) into the place in the file formerly occupied by the code.
• This new output file is sent back to the web server.
• The web server sends it along to the web browser.
• The web browser displays the output.
• Because the PHP code is parsed on the server, this method of code execution
is called server-side code.
13. Datatypes in PHP
• Data Types define the type of data a variable can store. PHP allows eight different
types of data types. There are pre-defined, user-defined, and special data types.
The predefined data types are:
• Boolean
• Integer
• Double
• String
The user-defined (compound) data types are:
• Array
• Objects
The special data types are:
• NULL
• resource
14. • The first five are called simple data types and the last three are compound data types:
• 1. Integer: Integers hold only whole numbers including positive and negative numbers,
i.e., numbers without fractional part or decimal point. They can be decimal (base 10),
octal (base 8), or hexadecimal (base 16). The default base is decimal (base 10). The octal
integers can be declared with leading 0 and the hexadecimal can be declared with leading
0x.
• // decimal base integers
• $deci1 = 50;
• $deci2 = 654;
• // octal base integers
• $octal1 = 07;
• // hexadecimal base integers
• $octal = 0x45;
• $sum = $deci1 + $deci2;
• echo $sum;
• echo "nn";
• //returns data type and value
• var_dump($sum)
• ?>
15. • 2. Double: Can hold numbers containing fractional or decimal parts including
positive and negative numbers or a number in exponential form. By default, the
variables add a minimum number of decimal places. The Double data type is the
same as a float as floating-point numbers or real numbers.
• <?php
• $val1 = 50.85;
• $val2 = 654.26;
• $sum = $val1 + $val2;
• echo $sum;
• echo "nn";
• //returns data type and value
• var_dump($sum)
• ?>
16. • 3. String: Hold letters or any alphabets, even numbers are included. These are
written within double quotes during declaration.
• <?php
• $name = “Computer";
• echo "The name of the Geek is $name n";
• echo "nn";
• //returns data type, size and value
• var_dump($name)
• ?>
17. • 4. Boolean: Boolean data types are used in conditional testing. Hold only two
values, either TRUE(1) or FALSE(0). Successful events will return true and
unsuccessful events return false. NULL type values are also treated as false in
Boolean. Apart from NULL, 0 is also considered false in boolean. If a string is
empty then it is also considered false in boolean data type.
• <?php
• if(TRUE)
• echo "This condition is TRUE";
• if(FALSE)
• echo "This condition is not TRUE";
• ?>
18. • 5. Array: Array is a compound data type that can store multiple values of the same
data type. Below is an example of an array of integers. It combines a series of data
that are related together.
• <?php
• $intArray = array( 10, 20 , 30);
• echo "First Element: $intArray[0]n";
• echo "Second Element: $intArray[1]n";
• echo "Third Element: $intArray[2]nn";
• //returns data type and value
• var_dump($intArray);
• ?>
19. • 6. Objects: Objects are defined as instances of user-defined classes that can hold both values and
functions and information for data processing specific to the class. When the objects are created, they
inherit all the properties and behaviours from the class, having different values for all the properties.
• Objects are explicitly declared and created from the new keyword.
• <?php
• class gfg {
• var $message
• function gfg($message) {
• $this->message = $message;
• }
• function msg() {
• return "This is an example of " . $this->message . "!";
• }
• }
• // instantiating a object
• $newObj = new gfg("Object Data Type");
• echo $newObj -> msg();
• ?>
20. • 7. NULL: These are special types of variables that can hold only one value i.e.,
NULL. We follow the convention of writing it in capital form, but it’s case-
sensitive. If a variable is created without a value or no value, it is automatically
assigned a value of NULL. It is written in capital letters.
• <?php
• $nm = NULL;
• echo $nm; // this will return no output
• // return data type
• var_dump($nm);
• ?>
21. • 8. Resources: Resources in PHP are not an exact data type. These are basically
used to store references to some function call or to external PHP resources. For
example, consider a database call. This is an external resource. Resource variables
hold special handles for files and database connections.
22. Operators in PHP
• Operators are used to perform operations on variables and values.
• PHP divides the operators in the following groups:
• Arithmetic operators
• Assignment operators
• Comparison operators
• Increment/Decrement operators
• Logical operators
• String operators
• Array operators
• Conditional assignment operators
23. Arithmetic Operators
• The PHP arithmetic operators are used with numeric values to perform common
arithmetical operations, such as addition, subtraction, multiplication etc.
24. Assignment Operators
• The PHP assignment operators are used with numeric values to write a value to a
variable.
• The basic assignment operator in PHP is "=". It means that the left operand gets
set to the value of the assignment expression on the right.
26. Increment/Decrement Operators
• The PHP increment operators are used to increment a variable's value.
• The PHP decrement operators are used to decrement a variable's value.
31. PHP variables
• Creating (Declaring) PHP Variables
• In PHP, a variable starts with the $ sign, followed by the name of the variable:
• <?php
$txt = "Hello world!";
$x = 5;
$y = 10.5;
?>
• After the execution of the statements above, the variable $txt will hold the value
Hello world!, the variable $x will hold the value 5, and the variable $y will hold
the value 10.5.
32. Rules for assigning name for variables
• A variable can have a short name (like x and y) or a more descriptive name
(age, carname, total_volume).
• 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)
33. Output Variables
• The PHP echo statement is often used to output data to the screen.
• The following example will show how to output text and a variable:
• <?php
• $txt = “google.com";
• echo "I love $txt!";
• ?>
34. Print
• The print statement can be used with or without parentheses: print or print().
• The differences are small: echo has no return value while print has a return value
of 1 so it can be used in expressions. echo can take multiple parameters (although
such usage is rare) while print can take one argument. echo is marginally faster
than print.
• <?php
• print "<h2>PHP is Fun!</h2>";
• print "Hello world!<br>";
• print "I'm about to learn PHP!";
• ?>
35. Static And Global Variables
• In PHP, variables can be declared anywhere in the script.
• The scope of a variable is the part of the script where the variable can
be referenced/used.
• PHP has three different variable scopes:
• local
• global
• static
36. Global and Local Scope
• A variable declared outside a function has a GLOBAL SCOPE and can only
be accessed outside a function.
• <?php
• $x = 5; // global scope
• function myTest() {
• // using x inside this function will generate an error
• echo "<p>Variable x inside function is: $x</p>";
• }
• myTest();
• echo "<p>Variable x outside function is: $x</p>";
• ?>
37. • A variable declared within a function has a LOCAL SCOPE and can only be
accessed within that function.
• <?php
• function myTest() {
• $x = 5; // local scope
• echo "<p>Variable x inside function is: $x</p>";
• }
• myTest();
• // using x outside the function will generate an error
• echo "<p>Variable x outside function is: $x</p>";
• ?>
38. PHP The global Keyword
• The global keyword is used to access a global variable from within the function.
• We have to use global keyword before the variables (inside the function).
• <?php
$x = 5;
$y = 10;
function myTest() {
global $x, $y;
$y = $x + $y;
}
myTest();
echo $y; // outputs 15
?>
39. • PHP also stores all global variables in an array called $GLOBALS[index]. The index
holds the name of the variable. This array is also accessible from within the functions and
can be used to update global variables directly.
• <?php
• $x = 5;
• $y = 10;
• function myTest() {
• $GLOBALS['y'] = $GLOBALS['x'] + $GLOBALS['y'];
• }
• myTest();
• echo $y; // outputs 15
• ?>
40. Static Keyword
• When a function is completed/executed, all of its variables are deleted. However,
sometimes we want a local variable NOT to be deleted. We need it for a further job.
• To do this, use the static keyword when you first declare the variable.
• <?php
• function myTest() {
• static $x = 0;
• echo $x;
• $x++;
• }
• myTest();
• myTest();
• myTest();
• ?>
41. Comments in PHP
• A comment in PHP code is a line that is not executed as a part of the program. Its only purpose is to be
read by someone who is looking at the code.
• Comments can be used to:
• Let others understand your code
• Remind yourself of what you did - Most programmers have experienced coming back to their own work
a year or two later and having to re-figure out what they did. Comments can remind you of what you
were thinking when you wrote the code
• PHP supports several ways of commenting:
• Single Line commenting
• <!DOCTYPE html>
• <html>
• <body>
• <?php
• // This is a single-line comment
• # This is also a single-line comment
• ?>
• </body>
• </html>
42. • Multiple Line Comments
• <!DOCTYPE html>
• <html>
• <body>
• <?php
• /*
• This is a multiple-lines comment block
• that spans over multiple
• lines
• */
• ?>
• </body>
• </html>
43. • Using comments to leave out parts of the code.
• <!DOCTYPE html>
<html>
<body>
<?php
// You can also use comments to leave out parts of a code line
$x = 5 /* + 15 */ + 5;
echo $x;
?>
</body>
</html>
Hinweis der Redaktion
An interpreted language is a programming language whose implementations execute instructions directly and freely, without previously compiling a program into machine-language instructions.
Server-side scripting languages are programming languages developed especially for creating HTML pages (or Web pages) on the server side
Web hosting is the activity of providing storage space for a website that is connected to the Internet. When you have a company that has space on servers and people buy space to store their websites so they can appear on the Internet,
WordPress is a free and open-source content management system written in PHP and paired with a MySQL or MariaDB database with supported HTTPS.
All HTML need to have a DOCTYPE declared. The DOCTYPE is not actually an element or HTML tag. It lets the browser know how the document should be interpreted, by indicating what version or standard of HTML (or other markup language) is being used.