TIOLE
TIOLE
FHUFJHKJHHSD
With PHP you are not limited to output HTML. You can output images or PDF
files. You can also output any text, such as XHTML and XML.
Why PHP?
PHP runs on various platforms (Windows, Linux, Unix, Mac OS X, etc.)
PHP is compatible with almost all servers used today (Apache, IIS, etc.)
PHP supports a wide range of databases
PHP is free. Download it from the official PHP resource: www.php.net
PHP is easy to learn and runs efficiently on the server side
Developed by Tim
Developed by Rasmus Lerdorf.
Berners-Lee.
It is used for the development of dynamic websites and It is used to create web
dynamic web applications. pages.
Steps:
Select your desired location, where you want to install XAMPP and then click
“Next”.
Click “Next” on the coming screens to proceed with the installation process.
Now, you will see the final screen. I would suggest that you keep the “start the
Control Panel” option checked. Click “Finish” to complete the installation
process. A new window will open shortly.
The XAMPP Control Panel has now started. Now, click “Start” button in
Apache and MySQL rows to begin.
You are now ready to start writing the code. Now all you need is an editor
like Notepad++ or Dreamweaver to write the code.
?>
Now, save the page as “test.php” in htdocs folder and click “Save” button.
Now, open a web browser and type localhost in the address bar. It will
automatically open the index file but if you type localhost/test.php, it will open
the page that we have saved.
<html>
<head>
</head>
<body>
<?php
?>
</body>
</html>
In this example, we use echo and print to show the same result. Here is the
output we get.
You can see that the two lines of 2+3 are displayed as output by using
different statements. Most of the professional programmers prefer to use echo
because echo can bring up multiple strings or values at the same time,
whereas print displays one statement at a time. Both echo and print can be
used with or without parentheses; print() or echo(). Also, it is to be noticed that
you can not see the sum of two numbers without using variables. The concept
of variables will be introduced along with PHP data types in the next tutorial.
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<?php
$a=99;
$b=”Calculus”;
?>
</body>
</html>
In this example, you can see that we have echoed the same string with double
quotes and single quotes. Here is the output.
When we use double quotes, it displays the string along with the values
assigned to variables $a and $b. However, when we use single quotes, it will
treat the whole statement as string and will display variables $a and $b. I will
touch upon the concept of variables in detail in the next tutorial as well.
Thats it! that is how you start your PHP scripts! In the upcoming weeks, I will
be discussing more about PHP; from the most basic tutorials to the most
advanced. I hope to see you around for more PHP tutorials.
In the meanwhile, you can sign up and deploy PHP on the revolutionary
managed Cloud Hosting Platform. Choose your cloud provider from some of
the best infrastructures around, namely Google Compute Engine,
DigitalOcean and Amazon Web Services. It will take you less than 6 minutes
to sign up, choose the cloud provider and deploy PHP on your selected cloud
provider. It is fast and secure. Plus, you are always covered with a 24/7
support team that never keeps you at bay!
Read
Discuss
Courses
Practice
Video
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.
How to run PHP programs ?
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.
Example: The “demo.php” file is saved in the htdocs folder.
PHP
<!DOCTYPE html>
<html>
<body>
<?php
?>
</body>
</html>
How to run PHP programs ?
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.
Syntax: php have special Syntax to write a code, as shown in below
<?php
//write your code here
?>
demo.php
PHP
<!DOCTYPE html>
<html>
<body>
<?php
?>
</body>
</html>
How to run PHP programs ?
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.
How to run PHP programs ?
<!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
1. GET method
2. POST method
Get and Post methods are the HTTP request methods used inside the <form> tag to
send form data to the server.
HTTP protocol enables the communication between the client and the server where a
browser can be the client, and an application running on a computer system that hosts
your website can be the server.
GET method
The GET method is used to submit the HTML form data. This data is collected by the
predefined $_GET variable for processing.
PlayNext
Mute
Current Time 0:07
Duration 18:10
Loaded: 4.40%
Â
Fullscreen
The information sent from an HTML form using the GET method is visible to everyone in
the browser's address bar, which means that all the variable names and their values will
be displayed in the URL. Therefore, the get method is not secured to send sensitive
information.
For Example
1. localhost/gettest.php?username=Harry&bloodgroup=AB+
The bold part in the above URL is the variables name and italic part contains the values
for their corresponding variable.
Note that only a limited amount of information can be sent using the GET method.
With the help of an example, let's understand how the GET method works-
Example
The below code will display an HTML form containing two input fields and a submit
button. In this HTML form, we used the method = "get" to submit the form data.
file: test1.html
1. <html>
2. <body>
3.
4. <form action = "gettest.php" method = "GET">
5. Username: <input type = "text" name = "username" /> <br>
6. Blood Group: <input type = "text" name = "bloodgroup" /> <br>
7. <input type = "submit" />
8. </form>
9.
10. </body>
11. </html>
Create gettest.php file, which will accept the data sent by HTML form.
file: gettest.php
1. <html>
2. <body>
3.
4. Welcome <?php echo $_GET["username"]; ?> </br>
5. Your blood group is: <?php echo $_GET["bloodgroup"]; ?>
6.
7. </body>
8. </html>
When the user will click on Submit button after filling the form, the URL sent to the
server could look something like this:
localhost/gettest.php?username=Harry&bloodgroup=AB-
Welcome Harry
Your blood group is: AB-
POST method
Similar to the GET method, the POST method is also used to submit the HTML form
data. But the data submitted by this method is collected by the predefined superglobal
variable $_POST instead of $_GET.
Unlike the GET method, it does not have a limit on the amount of information to be
sent. The information sent from an HTML form using the POST method is not visible to
anyone.
For Example
1. localhost/posttest.php
Note that the "post" method is more secure than the "get" method because the data sent
using the POST method is not visible to user.
With the help of an example, let's understand how the POST method works-
Example
The below code will display an HTML form containing two input fields and a submit
button. In this HTML form, we used the method = "post" to submit the form data.
file: test2.html
1. <html>
2. <body>
3.
4. <form action = "posttest.php" method = "post">
5. Username: <input type = "text" name = "username" /> <br>
6. Blood Group: <input type = "text" name = "bloodgroup" /> <br>
7. <input type = "submit" />
8. </form>
9.
10. </body>
11. </html>
Now create posttest.php file to accept the data sent by HTML form.
file: posttest.php
1. <html>
2. <body>
3.
4. Welcome <?php echo $_POST["username"]; ?> </br>
5. Your blood group is: <?php echo $_POST["bloodgroup"]; ?>
6.
7. </body>
8. </html>
When the user will click on Submit button after filling the form, the URL sent to the
server could look something like this:
localhost/posttest.php
Welcome Harry
Your blood group is: O+
1. Empty String
2. Validate String
3. Validate Numbers
4. Validate Email
5. Validate URL
6. 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.
1. if (emptyempty ($_POST["name"])) {
2. $errMsg = "Error! You didn't enter the Name.";
3. echo $errMsg;
4. } else {
5. $name = $_POST["name"];
6. }
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:
PlayNext
Unmute
Current Time 0:00
Duration 18:10
Loaded: 0.37%
Â
Fullscreen
1. $name = $_POST ["Name"];
2. if (!preg_match ("/^[a-zA-z]*$/", $name) ) {
3. $ErrMsg = "Only alphabets and whitespace are allowed.";
4. echo $ErrMsg;
5. } else {
6. echo $name;
7. }
Validate Number
The below code validates that the field will only contain a numeric value. For example
- Mobile no. If the Mobile no field does not receive numeric data from the user, the code
will display an error message:
1. $mobileno = $_POST ["Mobile_no"];
2. if (!preg_match ("/^[0-9]*$/", $mobileno) ){
3. $ErrMsg = "Only numeric value is allowed.";
4. echo $ErrMsg;
5. } else {
6. echo $mobileno;
7. }
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:
1. $email = $_POST ["Email"];
2. $pattern = "^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$^";
3. if (!preg_match ($pattern, $email) ){
4. $ErrMsg = "Email is not valid.";
5. echo $ErrMsg;
6. } else {
7. echo "Your valid email address is: " .$email;
8. }
The given code will help you to apply the length validation on user input:
1. $mobileno = strlen ($_POST ["Mobile"]);
2. $length = strlen ($mobileno);
3.
4. if ( $length < 10 && $length > 10) {
5. $ErrMsg = "Mobile must have 10 digits.";
6. echo $ErrMsg;
7. } else {
8. echo "Your Mobile number is: " .$mobileno;
9. }
Validate URL
The below code validates the URL of website provided by the user via HTML form. If the
field does not contain a valid URL, the code will display an error message, i.e., "URL is
not valid".
1. $websiteURL = $_POST["website"];
2. if (!preg_match("/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/
%=~_|]/i",$website)) {
3. $websiteErr = "URL is not valid";
4. echo $websiteErr;
5. } else {
6. echo "Website URL is: " .$websiteURL;
7. }
1. if (isset ($_POST['submit']) {
2. echo "Submit button is clicked.";
3. if ($_SERVER["REQUEST_METHOD"] == "POST") {
4. echo "Data is sent using POST method ";
5. }
6. } else {
7. echo "Data is not submitted";
8. }
Note: Remember that validation and verification both are different from each other.
Now we will apply all these validations to an HTML form to validate the fields. Thereby
you can learn in detail how these codes will be used to validation form.
When the above code runs on a browser, the output will be like the screenshot below:
Fill the registration form and click on the Submit button. If all required information is
provided correctly, the output will be displayed on the same page below the submit
button. See the screenshot below:
Remember that we have not used a database to store the data for registered users.