PHP Basics Introduction tutorial



PHP Introduction Tutorial


coding-zon-php-introduction

 Table of Contents


  1. PHP Introduction
  2. Web Application Architecture:
  3. Why PHP?
  4. PHP Program structure
  5. Using HTML tags
  6. Variables in PHP
  7. Comments  
  8. Joining with dot operator
  9. Data Types in PHP
  10. Starting Server
  11. Running program in a server
  12. Embedding HTML in PHP
  13. Embedding PHP in HTML


  PHP Introduction

  •  PHP Developed in 1995.
  •  PHP first known as Hypertext Preprocessor.
  •  PHP also called as Personal Homepage Tools(PHP Tools) from version 2.0.
  •  PHP scripts are executed on the server.
  •  PHP is a server-side scripting language, like ASP.
  •  PHP supports many databases (MySQL, Informix, Oracle, Sybase, Solid, PostgreSQL, Generic ODBC(MicroSoft’s), etc.)
  •  PHP is an open source software. It is free to download and use.
  •  PHP runs on different platforms (Windows, Linux, Unix, etc.)
  •  PHP is compatible with almost all servers used today (Apache, IIS, Tomcat etc.)
  •  PHP is easy to learn and runs efficiently on the server side.
  •  PHP designed for producing dynamic web pages.

 

Menu Top

 Web Application Architecture in PHP:

In Web-application we use different scripts in different layers.
 

1. Layer-1 (Front End)

     a. HTML, CSS, HTML5: To design static pages and create forms.
     b. JavaScript, jQuery: Client side scripting languages.
         JavaScript → To validate form fields,
         jQuery → To add Effects, Animations, Slideshows, Galleries to site.
 

2. Layer-2 (Middle tier)

        a. PHP Server-side Script language. To process form data.
 

3. Layer-3 (Back End)

        a. MySQL  To store user data for later use, MySQL database is used.
        b. SQL    
    In MySQL to manage data, SQL Language is used.

 

 

coding-zon-client-server-php
 

Why PHP?

  •  PHP can collect form data
  •  PHP reads and writes the files on the server
  •  Manage (add, delete, modify) data in a database.
  •  Access cookies variables and set cookies.
  •  Restrict users to access some pages of your website using sessions.
  •  It can encrypt data.

 

Move Up

PHP Program:

PHP is a scripting language. All programming statements written inside PHP tags(

<?php ..?>). It also supports using HTML tags inside PHP code.

Syn:


<?php
 
 statement 1;
 statement 2;  
 ..
 statement N; 
 
?>

 Example:

helloworld.php

<?php
 
print "Hello world";
 
?>

Using HTML tags in PHP

print "<h1>Hello world "</h1>";


  

Move TOC

Variables in PHP

Definition:
Variables are named memory locations to store and retrieve data.
In PHP, all variables start with '$' sign.
The variable name is case-sensitive.
Ex:

<?php
 
$city = "Noida";
$City = "Delhi";
 
echo  $City;  //Noida
echo  $city;  //Delhi 
?>

In the above, both variables are treated as different variables in PHP.

If you print $city output - 'Noida', if you print $City outputs 'Delhi'.


Variable naming rules:

  • It can be any length. 
  • It accepts only '_' underscore.
  • Should not start with a number, or any special characters.
  • Should not contain spaces.

 

Go Top

Using Variables in PHP


<?php
 
$name = 'John';
$address="USA";
 
echo $name, $address; //echo allows commas(,)
 
?>

Number variable

 <?php

    $num = 10;
    print ($num); //parenthesis optional
?>

Using Comments:

Comments helps the program more readable. Comments allows programmer can add some explanation about the logic.

  • Single line comment: prefixed with two slashes(//)

         //statement

  • Multiline comment: statements enclosed with /* .. */
  /* 
.... 
.... 
*/


Joining with dot operator

<?php
    $num = 10;
    $text = 'num = ';
    print ($text . $n); // print uses dot(.) for concatinating string with variable
?>

Example-2:

<?php
 
    $a = 10;
    $b = 20;
    $c = $a + $b;
    print ("sum=" . $c );
 
?>

Move Top

Constants in PHP: 

Stores read only values. Once the value set, it can't be changed.


    
  • Constant defined using define()-function. 
  • Once a constant defined, it can never be changed. 
  • Constant does not have '$'.
 
<?php
 
define("HIGH", 1000);
define("LOW", 100);
define("COMPANY", "XYZ Enterprises");
define("NL", "<br>"); // -->NL (NewLine)
 
echo MAX;             // 100
define("HIGH", 700);    // error
echo NL.COMPANY;
print "Maximum=". HIGH.NL. "Minimum=". LOW;
 
?>

 Data Types in PHP

  • Boolean 
  • Integers 
  • Floating point numbers 
  • Strings 
  • Arrays 
  • Objects ex: Date
 
<?php
 
$firstName = "Harry"; // a PHP String
$lastName = "Pottor"; // a PHP String
 
$age = 18; // a PHP Integer
 
$height = 5.4; // PHP Float
 
$hasDriversLicense = True; // a PHP Boolean
 
?>


Debugging– Feature:

 
  • PHP is not a strongly typed language(i.e., variables must be declared in the beginning before they are going to use in a program).

  • In PHP, Variables can be created anywhere in your code but they are case-sensitive.

    Ex: $name and $Name both are treated as different variables.

 

 


TOC

Running program in a Server

 All PHP programs are run in the server and sends result back to the client.

Starting Server

In this tutorial, we are using XMPP Server. You can download from here .

Open control panel, and click on the Apache server start button. If it is started, it will show as shown in the picture, with a green background color.

coding-zon-xampp-server-start

Type localhost in browser, then it will display dashboard, as shown in the below picture

 


 

Next..

Create a folder 'phpdemo' in C:\xampp\htdocs 

Save above given  helloworld.php file in a phpdemo folder.

Open the above file(helloworld.php) in Browser using the following URL:

http://localhost/phpdemo/helloworld.php 

 


Then you will the above output in browser .

Embedding HTML in PHP.

Within the PHP, you can use HTML tags. 

Ex: hello.php

<?php
print ("<h1>Hello World</h1>");

?> 

Embedding PHP in HTML.

Hello.php * if we use PHP code in HTML, it must save as PHP file.

<html>
<head>
<title>PHP Test</title>
</head>
<body>
<?php 
 
echo '<p>Hello World</p>';  
 
?>
</body>
</html>

Comments

Popular posts from this blog

Using javascript pass form variables to iframe src

Creating a new PDF by Merging PDF documents using TCPDF

Import excel file into mysql in PHP