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

PHP

PHP is a popular server-side scripting language used for web development. It allows generation of dynamic web page content and interaction with databases. PHP code is placed within <?php ?> tags and includes variables, data types, operators, and control structures. Arrays can hold multiple values of different types. Form data can be accessed via the $_POST and $_GET superglobals. Database connectivity in PHP can be established using PDO or MySQLi to execute queries and manage data.

Uploaded by

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

PHP

PHP is a popular server-side scripting language used for web development. It allows generation of dynamic web page content and interaction with databases. PHP code is placed within <?php ?> tags and includes variables, data types, operators, and control structures. Arrays can hold multiple values of different types. Form data can be accessed via the $_POST and $_GET superglobals. Database connectivity in PHP can be established using PDO or MySQLi to execute queries and manage data.

Uploaded by

smengesha838
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 22

PHP

Contents
• Introduction
• Install
• Syntax
• Variables
• Echo/print
• Forms
• Database connectivity
Introduction
• PHP (Hypertext Preprocessor) is a popular server-side scripting language used
primarily for web development. It is designed to create dynamic web pages and
interact with databases and other server-side technologies
• Server-Side Scripting: PHP is executed on the server side, meaning the PHP
code is processed on the web server before the resulting HTML is sent to the
client's web browser. This allows PHP to generate dynamic content that can
change based on user input or other conditions
• PHP can generate dynamic page content
• PHP can create, open, read, write, delete, and close files on the server
• PHP can collect form data
• PHP can send and receive cookies
• PHP can add, delete, modify data in your database
• PHP can be used to control user-access
• PHP can encrypt data
PHP Syntax
• Opening and Closing Tags: PHP code is typically enclosed within <?
php and ?> tags. For example:

• Comments: You can add comments in PHP to document your code or


disable certain sections. PHP supports both single-line and multi-line
comments:
• Variables: PHP variables start with a dollar sign $ followed by the variable name.
Variable names are case-sensitive and can contain letters, numbers, and
underscores.

• Data Types: PHP supports various data types, including:


• Strings: Enclosed in single quotes (') or double quotes (") like "Hello" or
'World’.
• Integers: Whole numbers like 42 or -10.
• Floats: Decimal numbers like 3.14 or -0.5.
• Booleans: true or false.
• Arrays: Ordered collections of values.
• Objects: Instances of custom-defined classes.
• NULL: Represents the absence of a value.
• Outputting Data: You can output data using the echo statement or print function:

• Operators: PHP supports various operators for arithmetic, comparison, logical


operations, and more. For example:
• Control Structures: PHP provides control structures for conditional
execution and loops. Some common control structures include:
• If-Else Statements, loops
• an array is a data structure that allows you to store multiple values in a single
variable. Arrays can hold values of different data types, such as strings, integers,
floats, or even other arrays. There are several ways to create and work with arrays
in PHP. Here are some examples:
• Creating an Array:
• You can create an array using the array() construct or the square bracket
syntax ([])
• Accessing Array Elements:
• You can access individual elements of an array using their corresponding
index. Indexing starts from 0 for the first element.

• Modifying Array Elements:


• You can modify the values of array elements by assigning new values to
specific indexes.
• Adding Elements to an Array:
• You can add new elements to an array by assigning a value to a non-existent index or
using the array_push() function.

• Array Functions:
• PHP provides several built-in functions to work with arrays. Here are some commonly used
ones:
• count($array): Returns the number of elements in an array.
• array_pop($array): Removes and returns the last element of an array.
• array_merge($array1, $array2): Merges two or more arrays into a single array.
• array_search($value, $array): Searches for a value in an array and returns its corresponding
index.
• array_keys($array): Returns an array containing all the keys of an associative array.
• array_values($array): Returns an array containing all the values of an associative array.
• These functions can help you manipulate and extract data from arrays efficiently.
• in PHP, an array can hold values of different data types in a single array. PHP
arrays are considered to be "loosely typed" or "dynamically typed," meaning they
can accommodate values of any data type

• In this example, the $myArray array contains elements of different types: an


integer (10), a string ("Hello"), a float (3.14), and a boolean (true). PHP allows
you to mix and match data types within an array.
• You can access individual elements of the array using their corresponding index
and perform operations based on their data type:
• Functions: PHP allows you to define functions for reusable code
blocks. Here's an example:

• Include and Require: PHP provides include and require statements to


include external PHP files into your code:
PHP Form Processing
• Handling forms in PHP involves retrieving form data, validating and sanitizing the
input, and performing the necessary actions based on the form submission
• Start by creating an HTML form using the <form> tag. Specify the method as
"POST" or "GET" and set the action attribute to the PHP script that will handle
the form submission
• Create a PHP file (e.g., "process-form.php") that will handle the form
submission. In this file, you can access the submitted form data using
the $_POST superglobal array.
Form Validation and Error Display:
• You can enhance the form handling script to include validation and display error
messages if the form data is invalid.
• Both GET and POST create an array (e.g. array( key1 => value1, key2 => value2,
key3 => value3, ...)). This array holds key/value pairs, where keys are the names of
the form controls and values are the input data from the user.
• Both GET and POST are treated as $_GET and $_POST. These are superglobals,
which means that they are always accessible, regardless of scope - and you can access
them from any function, class or file without having to do anything special.
• $_GET is an array of variables passed to the current script via the URL parameters.
• $_POST is an array of variables passed to the current script via the HTTP POST
method.
• Information sent from a form with the GET method is visible to everyone (all variable
names and values are displayed in the URL). GET also has limits on the amount of
information to send. The limitation is about 2000 characters. However, because the
variables are displayed in the URL, it is possible to bookmark the page. This can be useful
in some cases.
• GET may be used for sending non-sensitive data.
• Note: GET should NEVER be used for sending passwords or other sensitive information!
• Information sent from a form with the POST method is invisible to others (all
names/values are embedded within the body of the HTTP request) and has no limits on the
amount of information to send.
• Moreover POST supports advanced functionality such as support for multi-part binary
input while uploading files to server.
• However, because the variables are not displayed in the URL, it is not possible to
bookmark the page.
• To establish a database connectivity in PHP, you can use the PHP Data
Objects (PDO) extension or the MySQLi extension.
• Install and Configure Database:
• Before establishing a connection, make sure you have a MySQL database
installed and configured. Create a database and a table to work with. Make
note of the database host, name, username, and password, as you'll need them
to connect.
• For this course we use xampp
• PHP Database Connectivity:
• Start by creating a PHP file (e.g., "database.php") and establish a connection
to the database using PDO.
• Executing Database Queries:
• Once the database connection is established, you can execute queries to
retrieve, insert, update, or delete data from the database.
• Closing the Database Connection:
• After you're done with the database operations, it's good practice to close the
database connection
MySQLi extension
• To connect to a MySQL database using the MySQLi extension in PHP,
you can follow these steps:
• Once the database connection is established, you can execute queries
to retrieve, insert, update, or delete data from the database.

After you're done with the database operations, it's good practice to close the
database connection.

You might also like