+ All Categories
Home > Software > Php tutorials for Beginners

Php tutorials for Beginners

Date post: 14-Apr-2017
Category:
Upload: adeel990
View: 99 times
Download: 1 times
Share this document with a friend
39
By: MUHAMMAD ADEEEL
Transcript
Page 1: Php tutorials for Beginners

By: MUHAMMAD ADEEEL

Page 2: Php tutorials for Beginners

What is PHP ?The PHP Hypertext Preprocessor (PHP -- yes, the first "P" in the acronym does indeed

stand for PHP!)

Allows web developers to create dynamic content that interacts with databases.

PHP is a widely-used, free, and efficient alternative to competitors such as Microsoft's ASP.

Page 3: Php tutorials for Beginners

PHP is a widely-used, open source scripting language PHP scripts are executed on the server PHP is free to download and use 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

What is PHP?

Page 4: Php tutorials for Beginners

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

What is PHP?

Page 5: Php tutorials for Beginners

HTML CSS JAVASCRIPT JQUERY

You have to Learn Before PHP

Page 6: Php tutorials for Beginners

Development started in 1994, by Rasmus Lerdorf.

PHP originally abbreviation for ‘Personal Home Pages’, now ‘PHP Hypertext Preprocessor’

Open Source

Server-side Scripting(executes on Server.)

Used to make web application

HTML-embedded scripting language

Cross-platform

Main Features of PHP Simpler and faster to develop

applications

Interpreted Language

Loosely typed language

PHP is compatible with almost all servers used today (Apache, IIS, etc.)

PHP Support Many Databases like (Mysql ,SQL Server ,Oracle)

Page 7: Php tutorials for Beginners

Light-weight

Cross-platform compatible

Procedural / Object Oriented programming

Supports wide range of Databases

Supports wide variety of outputs

Open source

Advantages of PHP

Page 8: Php tutorials for Beginners

PHP Software

A Web Server (Apache , IIS)

An RDBMS (DATABASE Software) (MYSQL)

What We Need For PHP ?

Page 9: Php tutorials for Beginners

There Are Different Development Packages available for PHP.(Package:-collection Of Software's.)

These Are Some Development Packages:-

• XAMPP Server

• WAMP Server

• LAMP Server

• MAMP Server

• SAMP Server

How to Install PHP ?

Page 10: Php tutorials for Beginners

For XAMPP Apache visit: https://www.apachefriends.org/download.html

How to Install Web Servers

Page 11: Php tutorials for Beginners

For XAMPP Apache visit: http://www.wampserver.com/en/

How to Install Web Servers

Page 12: Php tutorials for Beginners

Just start Apache and Sql Services

Go to : Local Drive C:/wamp/www -- For Wamp

Go to : Local Drive C:/xampp/htdocs -- For Xampp

Then make new Folder with your name

After Installing Local Server

Page 13: Php tutorials for Beginners

Now Go to Dreamwaever/PHP Strom or any tool Create New .Php format file And save it to Local Drive C:/wamp/www/ with the

Custom name “mycode.php”

Go to your Browser write on the tab localhost will you’re your Project “AA” or you Custom Name Folder. Just click on this folder then click on “mycode.php” file. You code will be Run.

How to make PHP Page

Page 14: Php tutorials for Beginners

How to make PHP Page

Page 15: Php tutorials for Beginners

How to Run Php Code

Page 16: Php tutorials for Beginners

XAMPP (X Cross Platform) Provide APACHE , MYSQL , PHP , PERAL

WAMP (W Windows) Provide APACHE , MYSQL , PHP

LAMP (L LINUX)

MAMP (M Macintosh)

SAMP (S Sun Solaris)

Web Development Packages

Page 17: Php tutorials for Beginners

• written in the C programming language• Rasmus Lerdorf in 1994 • for use in monitoring his online resume and related

personal information.• For this reason, PHP originally stood for

• "Personal Home Page"

• Because php libraries are already compiled and processed.

• Person request any php page in browser address bar that request first go to server for example Apache is running on that server.

• Server interpret php files and return back response in form of HTML

Page 18: Php tutorials for Beginners

Most software we use have been compiled. This means that its computer code has been put through an application that translates it into a form your computer can understand. After a program has been compiled, it's nearly impossible to modify it or see exactly how it was created.

Open source software includes the source code along with the compiled code. People who write open source software actually encourage others to customize it

Cross-platform software can run on most or all systems with little or no modification

Page 19: Php tutorials for Beginners

Not limited to output HTML.

Abilities includes outputting images

PDF files and

even Flash movies

You can also output easily any text, such as XHTML and any other XML file.

OUTPUT

Page 20: Php tutorials for Beginners

Basic Syntax

<?php// PHP code goes here

?>

Page 21: Php tutorials for Beginners

Basic Syntax

<!DOCTYPE html><html><body>

<h1>My first PHP page</h1>

<?phpecho "Hello World!";

?>

</body></html>

PHP Code will include in <Body> Tag

Page 22: Php tutorials for Beginners

In PHP, a variable starts with the $ sign, followed by the name of the variable.

PHP has three different variable scopes: local global Static

PHP Variables<!DOCTYPE html>

<html><body>

<?php$txt = "W3Schools.com";

echo "I love " . $txt . "!";?>

</body></html>

Page 23: Php tutorials for Beginners

echo and print are more or less the same. They are both used to output data to the screen.

The differences are small: echo has no return value while print has a return value of 1 so it can be used in expressions. echo can take multiple parameters (although such usage is rare) while print can take one argument. echo is marginally faster than print.

PHP echo & Print Statements

<?phpecho "<h2>PHP is Fun!</h2>";echo "Hello world!<br>";echo "I'm about to learn PHP!<br>";echo "This ", "string ", "was ", "made ", "with multiple parameters.";?>

Page 24: Php tutorials for Beginners

PHP supports the following data types: String Integer Float (floating point numbers - also called double) Boolean Array Object NULL Resource

PHP Data Types

Page 25: Php tutorials for Beginners

Operators are used to perform operations on variables and values.

PHP divides the operators in the following groups:

Arithmetic operators Assignment operators Comparison operators Increment/Decrement operators Logical operators String operators Array operators

PHP Operators

Page 26: Php tutorials for Beginners
Page 27: Php tutorials for Beginners

A function name can start with a letter or underscore (not a number).

PHP Functions<?php

function familyName($fname) {    echo "$fname Refsnes.<br>";

}

familyName("Jani");familyName("Hege");familyName("Stale");

familyName("Kai Jim");familyName("Borge");

?>

Page 28: Php tutorials for Beginners

The PHP superglobals $_GET and $_POST are used to collect form-data.

PHP Form Handling

<html><body>

Welcome <?php echo $_GET["name"]; ?><br>Your email address is: <?php echo $_GET["email"]; ?>

</body></html>

<html><body>

Welcome <?php echo $_POST["name"]; ?><br>

Your email address is: <?php echo $_POST["email"]; ?>

</body></html>

Page 29: Php tutorials for Beginners

Both GET and POST create an array $_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.  Information sent from a form with the POST method is invisible

to others. 

GET VS POST

Page 30: Php tutorials for Beginners

to open files is with the fopen() function. The fread() function reads from an open

file. The fclose() function is used to close an

open file. The fgets() function is used to read a

single line from a file. The feof() function checks if the "end-of-

file" (EOF) has been reached. The fgetc() function is used to read a

single character from a file.

PHP File Open/Read/Close

<?php$myfile =

fopen("webdictionary.txt", "r");

// some code to be executed....

fclose($myfile);?>

Page 31: Php tutorials for Beginners

The fopen() function is also used to create a file. The fwrite() function is used to write to a file. The first parameter of fwrite() contains the name of the

file to write to and the second parameter is the string to be written.

PHP File Create/Write

Page 32: Php tutorials for Beginners

Several predefined variables in PHP 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.

The PHP superglobal variables are:

$GLOBALS $_SERVER $_REQUEST $_POST $_GET $_FILES $_ENV $_COOKIE $_SESSION

PHP Superglobal

Page 33: Php tutorials for Beginners

A cookie is often used to identify a user. A cookie is a small file that the server embeds on the user's computer. Each time the same computer requests a page with a browser, it will send the cookie too. With PHP, you can both create and retrieve cookie values.

A cookie is created with the setcookie() function.

PHP Cookie<?php

$cookie_name = "user";$cookie_value = "John Doe";setcookie($cookie_name, $cookie_value, time() +

(86400 * 30),"/"); // 86400 = 1 day?>

<html><body>

<?phpif(!

isset($_COOKIE[$cookie_name])) {    echo "Cookie named '" .

$cookie_name . "' is not set!";} else {

    echo "Cookie '" . $cookie_name . "' is set!<br>";    echo "Value is:

" . $_COOKIE[$cookie_name];}?>

</body></html>

Page 34: Php tutorials for Beginners

A session is a way to store information (in variables) to be used across multiple pages

A session is started with the session_start() function.

Session variables are set with the PHP global variable: $_SESSION.

PHP Sessions <?php

// Start the sessionsession_start();

?><!DOCTYPE html>

<html><body>

<?php// Set session variables

$_SESSION["favcolor"] = "green";$_SESSION["favanimal"] = "cat";

echo "Session variables are set.";?>

</body></html>

Page 35: Php tutorials for Beginners

PHP 5 and later can work with a MySQL database using: MySQLi extension (the "i" stands for improved) PDO (PHP Data Objects) PDO will work on 12 different database systems, where

as MySQLi will only work with MySQL databases.

How to Connect PHP with MySQL Database.

Page 36: Php tutorials for Beginners

For MySQLi Installation go to:  http://php.net/manual/en/mysqli.installation.php

For PDO Installation go to: http://php.net/manual/en/pdo.installation.php

How to Connect PHP with MySQL Database (cont.)

Page 37: Php tutorials for Beginners

Connect with MySQLi

<?php$servername = "localhost";$username = "username";$password = "password";

// Create connection$conn = new mysqli($servername, $username, $password);

// Check connectionif ($conn->connect_error) {    die("Connection failed: " . $conn->connect_error);} echo "Connected successfully";?>

Page 38: Php tutorials for Beginners

Connect with PDO<?php$servername = "localhost";$username = "username";$password = "password";

// Create connection$conn = mysqli_connect($servername, $username, $password);

// Check connectionif (!$conn) {    die("Connection failed: " . mysqli_connect_error());}echo "Connected successfully";?>

Page 39: Php tutorials for Beginners

Thank You…..


Recommended