PHP
PHP
PHP, which stands for PHP: Hypertext Preprocessor, is a widely-used open-source scripting language
designed primarily for web development1. It was created by Rasmus Lerdorf in 1993 and released in
19953. PHP is known for its ability to be embedded into HTML, making it a powerful tool for creating
dynamic web pages2.
1. Server-Side Execution: PHP code is executed on the server, and the result is sent to the client's
browser as plain HTML1. This means that the client does not see the PHP code, only the output.
2. Open Source: PHP is free to download and use, making it accessible to a wide range of
developers1.
3. Cross-Platform: PHP runs on various platforms, including Windows, Linux, Unix, and macOS1.
4. Database Support: PHP supports a wide range of databases, including MySQL, PostgreSQL,
Oracle, and more1.
5. Ease of Learning: PHP is easy to learn for beginners and offers advanced features for
professional programmers2.
How to run PHP programs ? XAMPP is a Web development tool, created by Apache, that makes it easy
to run PHP (Personal Home Pages) scripts on your computer locally. Installation of XAMPP Server on
windows is easy as compared to manual installation of a web server and PHP required a lot of in-depth
configuration knowledge. XAMPP package installs MySQL, FileZilla, Mercury, Perl, and Tomcat along with
Web Server and PHP, with these applications you can test the full website on your desktop. You don’t
need to upload it every time on an online Web server.
Step 1: First of all, open the Apache Friends website and download XAMPP for Windows, and install it.
Step 2: Start the XAMPP Program Control Panel. Click on the “Start” button next to the “Apache” to start
your Apache Web Server. Also, start “MySQL” if your PHP programs depend on a MySQL database to run.
Step 3: Place your PHP files in the “htdocs” folder located under the “XAMPP” folder on your drive (i.e.
C/D/E etc). The initial path is “your_drive_letter:\xampp\htdocs” for your Web server. Make sure that
your PHP files are saved as a “.php” file extension.
PHP<!DOCTYPE html>
<html>
<body>
<?php
?>
</body>
</html>
Step 4: Open up any web browser and enter “localhost/filename”. This will open the list of all the files
and folders stored under the “htdocs” folder on your computer. Click on the link to a PHP file and open it
to run a program.
Example: The file “demo.php” file is placed inside the “htdocs” folder. If you want to run it, open any
web browser and enter “localhost/demo.php” and press enter. Your program will run.
<?php
//write your code here
?>
demo.php
PHP
<!DOCTYPE html>
<html>
<body>
?>
</body>
</html>
Step 5: You can create any folder to test PHP files under the “htdocs” folder. If you create a specific
folder then you need to use the address as “localhost/foldername” to open them in your browser.
Example: The “demo.php” file is placed inside the “gfgdemo” folder. Enter
“localhost/gfgdemo/demo.php” in your browser and press enter, your program will be run.
PHP is case sensitive language, in that variables and functions, tags are case sensitive but classes are not
case sensitive. Below the example of case sensitivity of php.
Example 1: echo.php
PHP
<!DOCTYPE>
<html>
<body>
<?php
?>
</body>
</html>
In above code we write three different types of echo methods, see the output in the image.
Output:
Example 2: color.php
PHP
<html>
<body>
<?php
$color = "black";
?>
</body>
</html>
In the above example, the variable name is case sensitive, so it gives error.
Output:
My car is
My dog is black
My Phone is
Features of PHP
Over the years, PHP has incorporated numerous features. It is being
consistently upgraded with new features and code revisions. In this chapter,
let's highlight some of the key features of PHP:
PHP employs security algorithms like Sha1 and MD5 to encrypt strings.
Additionally, functions like filter_var and strip_tags contribute in maintaining a
secure environment for the users. PHP also supports secure communication
protocols like HTTPS.
Data Types
Here is a list of all the available data types and examples when creating variables in PHP:
• array: nodes of data assigned with indexes, or positions within the contained array.
PHP is not strict with data types. A variable assigned as a string could later be recreated as a numeric
value or vice versa.
Summary: in this tutorial, you will learn about PHP data types including scalar types, compound types,
and special types.
A type specifies the amount of memory that allocates to a value associated with it. A type also
determines the operations that you can perform on it.
PHP has ten primitive types including four scala types, four compound types, and two special types:
Scalar types
• bool
• int
• float
• string
Compound types
• array
• object
• callable
• iterable
Special types
• resource
• null
Scalar types
A variable is a scalar when it holds a single value of the type integer, float, string, or boolean.
Integer
Integers are whole numbers defined in the set {…-3,-2-,-1,0,1,2,3…}. The size of the integer depends on
the platform where PHP runs.
The constant PHP_INT_SIZE specifies the size of the integer on a specific platform. PHP uses the int
keyword to denote the integer type.
<?php
$count = 0;
$max = 1000;
Float
Floats are floating-point numbers, which are also known as floats, doubles, or real numbers.
PHP uses the IEEE 754 double format to represent floats. Like other programming languages, floats have
limited precision.
PHP uses the float keyword to represent the floating-point numbers. The following example illustrates
the floating-point numbers in PHP:
<?php
$price = 10.25;
Boolean
Boolean represents a truth value that can be either true or false. PHP uses the bool keyword to
represent the Boolean type.
The bool type has two values true and false. Since keywords are case-insensitive, you can
use true, True, TRUE, false, False, and False to indicate boolean values.
<?php
$is_admin = true;
When you use the values of other types in the boolean context, such as if-else and switch-
case statements, PHP converts them to the boolean values.
• The null.
The values that are not one of these falsy values above are true.
String
A string is a sequence of characters surrounded by single quotes (‘) or double quotes (“). For example:
<?php
Compound types
Compound data includes the values that contain more than one value. PHP has two compound types
including array and object.
Array
An array is an ordered map that associates keys with values. For example, you can define a list of items in
a shopping cart like this:
<?php
The $carts array contains three string values. It maps the index 0, 1, and 2 to the values 'laptop', 'mouse',
and 'keyboard'. The $carts is called an indexed array because it uses numeric indexes as keys.
<?php
Besides numeric indexes, you can use strings as keys for the array elements. These arrays are known
as associative arrays. For example:
<?php
$prices = [
To access an element in an associative array, you specify the key in the square brackets. For example:
<?php
echo $prices['mouse']; // 50
Object
An object has properties. For example, a person object may have the first name, last name, and age
properties.
An object also has behaviors, which are known as methods. For example, a person object can have a
method called getFullName() that returns the full name.
Special types
Null
The null type has one value called null that represents a variable with no value.
Resource
The resource type holds a reference to an external resource, e.g. a filehandle or a database connection.
Summary
• PHP has four scalar types, four compound types, and two special types.
• ………………………………….
XAMPP PANEL
After that create the database in the PHPMyAdmin. Open the below URL.
http://localhost/phpmyadmin/index.php
Creating the database:
Create the table: Execute the SQL query in the “gfg” database.
CREATE TABLE student
(
name varchar(20),
branch varchar(20),
roll_no INT
);
Enter the data in the table.
INSERT INTO `student` ( `name`, `branch`, `roll_no`)
VALUES ( 'Rohan', 'CSE', '1' );
After the data is inserted, the table will look like this.
PHP Code: Run the PHP script as shown below to fetch the data from the
database.
• PHP
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "gfg";
// connect the database with the server
// if error occurs
exit();
$result = ($conn->query($sql));
$row = [];
if ($result->num_rows > 0)
$row = $result->fetch_all(MYSQLI_ASSOC);
?>
<!DOCTYPE html>
<html>
<style>
td,th {
margin: 5px;
text-align: center;
</style>
<body>
<table>
<thead>
<tr>
<th>Name</th>
<th>Branch</th>
<th>Roll Number</th>
</tr>
</thead>
<tbody>
<?php
if(!empty($row))
foreach($row as $rows)
?>
<tr>
<?php } ?>
</tbody>
</table>
</body>
</html>
<?php
mysqli_close($conn);
?>
php
Copy code
<!DOCTYPE html>
<html>
<head>
</head>
<body>
</form>
</body>
</html>
php
Copy code
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (empty($_POST["username"])) {
} else {
$username = test_input($_POST["username"]);
if (empty($_POST["email"])) {
} else {
$email = test_input($_POST["email"]);
}
// Sanitize input
function test_input($data) {
?>
Explanation
1. HTML Form: We create a simple form with fields for the username and email, plus span tags for
error messages.