

































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
Php use Php application Php features etc
Typology: Study notes
1 / 41
This page cannot be seen from the preview
Don't miss anything!
PHP AND MYSQL
UNIT-
INTORDUTION TO PHP
INTRODUCTION TO PHP:
What is PHP
PHP is an open-source, interpreted, and object-oriented scripting language that can be executed at the server-side. PHP is well suited for web development. Therefore, it is used to develop web applications (an application that executes on the server and generates the dynamic page.).
PHP was created by Rasmus Lerdorf in 1994 but appeared in the market in 1995. PHP 7.4.0 is the latest version of PHP, which was released on 28 November. Some important points need to be noticed about PHP are as followed:
o PHP stands for Hypertext Preprocessor.
o PHP is an interpreted language, i.e., there is no need for compilation. o PHP is faster than other scripting languages, for example, ASP and JSP.
o PHP is a server-side scripting language, which is used to manage the dynamic content of the website.
o PHP can be embedded into HTML. o PHP is an object-oriented language.
o PHP is an open-source scripting language.
o PHP is simple and easy to learn language.
Why use PHP
PHP is a server-side scripting language, which is used to design the dynamic web applications with MySQL database.
o It handles dynamic content, database as well as session tracking for the website.
PHP AND MYSQL
o You can create sessions in PHP.
o It can access cookies variable and also set cookies. o It helps to encrypt the data and apply validation.
o PHP supports several protocols such as HTTP, POP3, SNMP, LDAP, IMAP, and many more.
o Using PHP language, you can control the user to access some pages of your website. o As PHP is easy to install and set up, this is the main reason why PHP is the best language to learn.
o PHP can handle the forms, such as - collect the data from users using forms, save it into the database, and return useful information to the user. For example - Registration form.
PHP Features
PHP is very popular language because of its simplicity and open source. There are some important features of PHP given below:
Performance:
PHP script is executed much faster than those scripts which are written in other languages such as JSP and ASP. PHP uses its own memory, so the server workload and loading time is automatically reduced, which results in faster processing speed and better performance.
Open Source:
PHP source code and software are freely available on the web. You can develop all the versions of PHP according to your requirement without paying any cost. All its components are free to download and use.
PHP AND MYSQL
PHP is widely used in web development nowadays. PHP can develop dynamic websites easily. But you must have the basic the knowledge of following technologies for web development as well.
o HTML
o CSS
o JavaScript
o Ajax o XML and JSON
o jQuery
How to run PHP code Generally, a PHP file contains HTML tags and some PHP scripting code. It is very easy to create a simple PHP example. To do so, create a file and write HTML tags + PHP code and save this file with .php extension
All PHP code goes between the php tag. It starts with . The syntax of PHP tag is given below:
How to run PHP programs in XAMPP
How to run PHP programs in XAMPP PHP is a popular backend programming language. PHP programs can be written on any editor, such as - Notepad, Notepad++, Dreamweaver, etc. These programs save with .php extension, i.e., filename.php inside the htdocs folder.
For example - p1.php.
As I'm using window, and my XAMPP server is installed in D drive. So, the path for the htdocs directory will be "D:\xampp\htdocs".
PHP AND MYSQL
PHP program runs on a web browser such as - Chrome, Internet Explorer, Firefox, etc. Below some steps are given to run the PHP programs.
Step 1: Create a simple PHP program like hello world.
Step 2: Save the file with hello.php name in the htdocs folder, which resides inside the xampp folder.
Step 3: Run the XAMPP server and start the Apache and MySQL.
Step 4: Now, open the web browser and type localhost http://localhost/hello.php on your browser window.
Step 5: The output for the above hello.php program will be shown as the screenshot below:
Most of the time, PHP programs run as a web server module. However, PHP can also be run on CLI (Command Line Interface)
INSTALLATION AND CONFIGURATION
Install PHP
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:
o WAMP for Windows
o LAMP for Linux
o MAMP for Mac
o SAMP for Solaris o FAMP for FreeBSD
PHP AND MYSQL
Step 2: After downloading XAMPP, double click on the downloaded file and allow XAMPP to make changes in your system. A window will pop-up, where you have to click on the Next button.
Step 3: Here, select the components, which you want to install and click Next.
PHP AND MYSQL
Step 4: Choose a folder where you want to install the XAMPP in your system and click Next.
PHP AND MYSQL
Step 7: A finish window will display after successful installation. Click on the Finish button.
PHP AND MYSQL
Step 8: Choose your preferred language.
Step 9: XAMPP is ready to use. Start the Apache server and MySQL and run the php program on the localhost.
How to run PHP programs on XAMPP, see in the next tutorial.
Step 10: If no error is shown, then XAMPP is running successfully.
PHP AND MYSQL
code for you. [3]Mostly because you are not allowed to use a > inside your tags if you wish to be compliant, but who wants to write code like if( $a > 5 )...? However, many of these same HTML editors recognize another mechanism (no more legal than PHP's) for embedding code—that of Microsoft's Active Server Pages (ASP). Like PHP, ASP is a method for embedding server-side scripts within documents. If you want to use ASP-aware tools to edit files that contain embedded PHP, you can use ASP-style tags to identify PHP regions. The ASP-style tag is the same as the SGML-style tag, but with % instead of ?: <% echo "Hello, world"; %> In all other ways, the ASP-style tag works the same as the SGML-style tag. docstore.mik.ua/orelly/webprog/php/ch02_07.html
In PHP, keyword (e.g., echo, if, else, while), functions, user-defined functions, classes are not case-sensitive. However, all variable names are case-sensitive.
In the below example, you can see that all three echo statements are equal and valid:
How to use PHP in Html
If we want to use PHP in the Html document, then we have to follow the steps which are given below. Using these simple steps, we can easily add the PHP code.
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.
PHP AND MYSQL
Step 2: Now, we have to place the cursor in any tag of the
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.
PHP AND MYSQL
o nowrap: Using this value, it treats white space between the element as normal, but it suppresses the line breaks or the text wrapping in the source. o pre: It does not collapse the white spaces they are preserved. The lines are only discontinued by the new line characters and break tags mentioned in the website's source.
o pre-wrap: The sequences of white space are preserved. It is similar to pre as the sequence of white-space are preserved. The lines are either broken on the new line or due to the break tag in the source code. The only difference is that it requires filling the line boxes too.
o pre-line: The value collapses the white spaces in the content. The lines are broken due to the new line characters or the break tag. It is necessary to fill the line boxes. o break-spaces: The behavior of break spaces is somewhat identical to pre-wrap; the key difference between both the values are as follows:
o Any sequence of preserved white space will occupy space. It also includes the end of a lin
SENDING DATA TO WEB BROWSER PHP - GET & POST Methods
There are two ways the browser client can send information to the web server.
The GET Method The POST Method
Before the browser sends the information, it encodes it using a scheme called URL encoding. In this scheme, name/value pairs are joined with equal signs and different pairs are separated by the ampersand.
name1=value1&name2=value2&name3=value
Spaces are removed and replaced with the + character and any other nonalphanumeric characters are replaced with a hexadecimal values. After the information is encoded it is sent to the server.
The GET Method
The GET method sends the encoded user information appended to the page request. The page and the encoded information are separated by the? character.
http://www.test.com/index.htm?name1=value1&name2=value The GET method produces a long string that appears in your server logs, in the browser's Location: box. The GET method is restricted to send upto 1024 characters only.
PHP AND MYSQL
Never use GET method if you have password or other sensitive information to be sent to the server. GET can't be used to send binary data, like images or word documents, to the server. The data sent by GET method can be accessed using QUERY_STRING environment variable. The PHP provides $_GET associative array to access all the sent information using GET method.
Try out following example by putting the source code in test.php script.
"; echo "You are ". $_GET['age']. " years old.";exit(); } ?>
It will produce the following result −
The POST Method
The POST method transfers information via HTTP headers. The information is encoded as described in case of GET method and put into a header called QUERY_STRING.
The POST method does not have any restriction on data size to be sent. The POST method can be used to send ASCII as well as binary data. The data sent by POST method goes through HTTP header so security depends on HTTP protocol. By using Secure HTTP you can make sure that your information is secure. The PHP provides $_POST associative array to access all the sent information using POST method.
Try out following example by putting the source code in test.php script.
PHP AND MYSQLPHP Multi 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.
PHP data types are used to hold different types of data or values. PHP supports 8 primitive data types that can be categorized further in 3 types:
PHP Data Types: Scalar Types
It holds only single value. There are 4 scalar data types in PHP.
PHP Data Types: Compound Types
It can hold multiple values. There are 2 compound data types in PHP.
PHP Data Types: Special Types
PHP AND MYSQL
There are 2 special data types in PHP.
PHP Boolean
Booleans are the simplest data type works like switch. It holds only two values: TRUE (1) or FALSE (0). It is often used with conditional statements. If the condition is correct, it returns TRUE otherwise FALSE.
Example:
Output:
This condition is TRUE.
PHP Integer
Integer means numeric data with a negative or positive sign. It holds only whole numbers, i.e., numbers without fractional part or decimal points.
Rules for integer:
o An integer can be either positive or negative.
o An integer must not contain decimal point. o Integer can be decimal (base 10), octal (base 8), or hexadecimal (base 16).
o The range of an integer must be lie between 2,147,483,648 and 2,147,483,647 i.e., - 2^31 to 2^31.
Example: