Top Banner
PHP
32

PHP. Why should we learn web programming? No need write socket programming. - You can forget TCP/IP & OSI layers. - Web server handles socket tasks for.

Dec 27, 2015

Download

Documents

Abel Johnston
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: PHP. Why should we learn web programming? No need write socket programming. - You can forget TCP/IP & OSI layers. - Web server handles socket tasks for.

PHP

Page 2: PHP. Why should we learn web programming? No need write socket programming. - You can forget TCP/IP & OSI layers. - Web server handles socket tasks for.

Why should we learn web programming?

No need write socket programming.- You can forget TCP/IP & OSI layers.- Web server handles socket tasks for us.

Page 3: PHP. Why should we learn web programming? No need write socket programming. - You can forget TCP/IP & OSI layers. - Web server handles socket tasks for.

Why should we learn web programming?

• Ability to develop variety of systems, intranet & extranet.

• - Inventory control system.• - Accounting application.• - Financial Analysis tools.• - Portal website.• - E-Commerce website.• - Game on facebook.• - etc.

Page 4: PHP. Why should we learn web programming? No need write socket programming. - You can forget TCP/IP & OSI layers. - Web server handles socket tasks for.

HTTP is about Request / Response.

Page 5: PHP. Why should we learn web programming? No need write socket programming. - You can forget TCP/IP & OSI layers. - Web server handles socket tasks for.

What programs are inside web server?

• Operating System such as Windows, Fedora, Ubuntu.- Handles communications between tenant

programs(programs that run on top of OS) and machine.

• Web Server Program such as Apache, IIS- Listening for request which is come from web browser

on port 80. (Normally HTTP port)- Send response back to web browser.• Language extension such as PHP, Perl, Python, Ruby.- Binding to Web Server Program.- Processing request, response and data from database.

Page 6: PHP. Why should we learn web programming? No need write socket programming. - You can forget TCP/IP & OSI layers. - Web server handles socket tasks for.

What programs are inside Database server?

• Operating System such as Windows, Fedora, Ubuntu.- Handles communications between tenant

programs(programs that run on top of OS) and machine.

• Database Server Program such as MySQL, Oracle.- Listening for query (Command) which is come from

WebServer on port 3306. (Normally MySQL port)- Send data back or update data itself.• We can install Database Server Program inside the

samemachine as Web Server but it is not recommended.

Page 7: PHP. Why should we learn web programming? No need write socket programming. - You can forget TCP/IP & OSI layers. - Web server handles socket tasks for.

Why PHP & MySQL?• Popular stack people use all over the world so

when you get stuck you just google your problem.

• PHP is free of charge. MySQL is also free.• Your code can deploy on both Windows and

Linux servers.• Most web hosting services support.• Coding PHP is fast and easy.• MySQL is quite stable and fast. (For large scale of

datas you have to use some techniques to optimize it)

Page 8: PHP. Why should we learn web programming? No need write socket programming. - You can forget TCP/IP & OSI layers. - Web server handles socket tasks for.

PHP (PHP: Hypertext Preprocessor)

PHP is a server-side scripting language intended to help web developers build dynamic web sites quickly.

Page 9: PHP. Why should we learn web programming? No need write socket programming. - You can forget TCP/IP & OSI layers. - Web server handles socket tasks for.

How does PHP work?

• PHP scripts are executed on the server, before the web page is displayed to the user (this is what we mean by "server-side"). The user only sees the end result, which consists of client-side markup and scripts (i.e. HTML, JavaScript, CSS etc). Therefore, the user/browser doesn't actually see any PHP code. If the user views the source code, all they would see is HTML, JavaScript, CSS etc - they wouldn't see any PHP code.

Page 10: PHP. Why should we learn web programming? No need write socket programming. - You can forget TCP/IP & OSI layers. - Web server handles socket tasks for.

• PHP is scripts (lines of command) which will be executed when web server has received REQUEST.

• After all PHP scripts completely executed, web server will send the result (of executed) back to web browser as RESPONSE.

Page 11: PHP. Why should we learn web programming? No need write socket programming. - You can forget TCP/IP & OSI layers. - Web server handles socket tasks for.

PHP Coding Structure

Page 12: PHP. Why should we learn web programming? No need write socket programming. - You can forget TCP/IP & OSI layers. - Web server handles socket tasks for.

Creating PHP file

• Pain text file (No rich text file such as a file created with MS Word or Wordpad).

• HTML and PHP code inside.• File has to be saved with .php extension.

Page 13: PHP. Why should we learn web programming? No need write socket programming. - You can forget TCP/IP & OSI layers. - Web server handles socket tasks for.

Scripting blocks

• Every block of PHP code must start with <?php and end with ?>

• <? and ?> can be used instead but someservers are not support this style so it is better

to use <?php and ?>• PHP blocks normally embed to HTML code to

control how HTML text should be rendered.

Page 14: PHP. Why should we learn web programming? No need write socket programming. - You can forget TCP/IP & OSI layers. - Web server handles socket tasks for.

Semi colon ;

• You need to place a semi-colon (;) at the end of each line of PHP code. This tells the server that a particular statement has finished.

Page 15: PHP. Why should we learn web programming? No need write socket programming. - You can forget TCP/IP & OSI layers. - Web server handles socket tasks for.

<!-- example1.php --><html><head><title>PHP Syntax Example</title></head><body><?phpprint “PHP is Pretty Hot Programming!”;?></body></html>

Page 16: PHP. Why should we learn web programming? No need write socket programming. - You can forget TCP/IP & OSI layers. - Web server handles socket tasks for.

Commenting

• Use this // Comment here for single line comment.

• Use this /* Comment multiple lines */for multiple lines comment.• Benefits to explain your idea, info, warning to

other programmers who read your code.

Page 17: PHP. Why should we learn web programming? No need write socket programming. - You can forget TCP/IP & OSI layers. - Web server handles socket tasks for.

<?php/*This file name is example2.php*/?><html><head><title>PHP Syntax Example</title></head><body><?php// show a cool word to visitorprint “PHP is Pretty Hot Programming!”;print “ Isn’t it?”; // Ask visitor if they agree.?></body></html>

Page 18: PHP. Why should we learn web programming? No need write socket programming. - You can forget TCP/IP & OSI layers. - Web server handles socket tasks for.

Variable

• Variables are named "containers" that allow you to store a value. This value can then be used by other parts of the application, simply by referring to the variable's name. For example, the application could display the contents of the variable to the user.

• In PHP, variable names must start with a dollar sign ($). For example:

Page 19: PHP. Why should we learn web programming? No need write socket programming. - You can forget TCP/IP & OSI layers. - Web server handles socket tasks for.

<?php$myVariable = "PHP is too easy for us";print $myVariable;?>

Page 20: PHP. Why should we learn web programming? No need write socket programming. - You can forget TCP/IP & OSI layers. - Web server handles socket tasks for.

Variable types

• There are 2 kinds of type: Simple and Complex• Simple is integer, float and boolean, string.• Complex is array type and object type.

Page 21: PHP. Why should we learn web programming? No need write socket programming. - You can forget TCP/IP & OSI layers. - Web server handles socket tasks for.

Integer

• Integer variables are able to hold a whole number. Negative values can be assigned by placing the minus (-) sign after the assignment operator and before the number.

Page 22: PHP. Why should we learn web programming? No need write socket programming. - You can forget TCP/IP & OSI layers. - Web server handles socket tasks for.

<?php$variable1 = -2;$variable2 = 9;$variable3 = $variable1 + $variable2;print $variable3;?>

Page 23: PHP. Why should we learn web programming? No need write socket programming. - You can forget TCP/IP & OSI layers. - Web server handles socket tasks for.

Float

• Floating point variables contain numbers that require the use of decimal places.

<?php$variable1 = -4234.2233;$variable2 = 0.0999;$variable3 = $variable1 * $variable2;print $variable3;?>

Page 24: PHP. Why should we learn web programming? No need write socket programming. - You can forget TCP/IP & OSI layers. - Web server handles socket tasks for.

Boolean

• PHP boolean type variables hold a value of true or false and are used to test conditions such as whether some part of a script was performed correctly.

<?php$variable1 = true;$variable2 = false;$variable3 = 1;$variable4 = 0;$variable5 = $variable1 && $variable4;print $variable5;?>

Page 25: PHP. Why should we learn web programming? No need write socket programming. - You can forget TCP/IP & OSI layers. - Web server handles socket tasks for.

String

• The string variable type is used to hold strings of characters such as words and sentences.

• A string can be assigned to variable either by surrounding it in single quotes (') or double quotes ("). If your string itself contains either single or double quotes you should use the opposite to wrap the string:

Page 26: PHP. Why should we learn web programming? No need write socket programming. - You can forget TCP/IP & OSI layers. - Web server handles socket tasks for.

<?php$variable1 = “Hello Tony”;$variable2 = ‘Hello Tony’;$variable3 = “Hello ‘Tony’ ”;$variable4 = ‘Hello “Tony” ’;?>

Page 27: PHP. Why should we learn web programming? No need write socket programming. - You can forget TCP/IP & OSI layers. - Web server handles socket tasks for.

Array

• PHP Arrays provide a way to group together many variables such that they can be referenced and manipulated using a single variable. An array is list of variables.

Page 28: PHP. Why should we learn web programming? No need write socket programming. - You can forget TCP/IP & OSI layers. - Web server handles socket tasks for.

<?php$ar1 = array(); // Creating empty array (nothing inside)// Create an array with various types inside$ar2 = array(1, 2.5, ‘Dog’, false);// Access and print value from array slot#2print $ar2[2];print ‘<br />’; // For visibility, add new line looks$ar3 = array(‘A’=>80, ‘B’=>70, ‘F’=>‘Fail’);// Access and print value from array slot named ’B’print $ar3[‘B’];?>

Page 29: PHP. Why should we learn web programming? No need write socket programming. - You can forget TCP/IP & OSI layers. - Web server handles socket tasks for.

Hello.php<?php$name1 = '';$name2 = '';if(isset($_GET['param1'])) $name1 = $_GET['param1'];if(isset($_GET['param2'])) $name2 = $_GET['param2'];?><html><head><title>Hello World!</title></head><body>Hello <?php print $name1; ?><?php if($name2 != '') print 'and

'; ?><?php print $name2; ?>!</body></html>

Page 30: PHP. Why should we learn web programming? No need write socket programming. - You can forget TCP/IP & OSI layers. - Web server handles socket tasks for.

OperatorsOperator Description Example Result

+ Addition 23 + 7 30

- Subtraction 90.34 - 1.11 89.23

* Multiplication 34232.43434 * 0 0

/ Division 15 / 3 5

% Modulus (Division remainder) 10 % 3 1

++ Increment $a = 99;++$a

100

-- Decrement $b = 50;--$b;

49

==,!= is equal to / is not equal to 80 == 6 FALSE

> , < is greater than / is less than 7 > 3 TRUE

&& and (5==5 && 6==6) TRUE

|| or (5==5 || 6==8) FALSE

! not !(0 > 1) TRUE

. string concatenation PH’ . ‘P’ . “!” PHP!

Page 31: PHP. Why should we learn web programming? No need write socket programming. - You can forget TCP/IP & OSI layers. - Web server handles socket tasks for.

Submitting FORM data to PHP page

• You have 2 php files.• p1.php is a sending page• p2.php is a receiving page• p1.php has FORM tag with action=”p2.php” attribute• p1.php has input tags with name=”anyname”

attribute• p2.php has php code to get datas which will be

submitted from p1.php• p2.php gets datas by referring to $_GET[‘anyname’]

Page 32: PHP. Why should we learn web programming? No need write socket programming. - You can forget TCP/IP & OSI layers. - Web server handles socket tasks for.

// p1.php

<form action ="p2.php" method=get>

Email: <input type="text" name="mail"> <br>

Name : <input type="text" name="name" > <br>

<textarea name="comment" cols=40 rows=10></textarea> <br>

<input type="submit" value="Submit"> </form>

// p2.php

<?php

$email = $mail;

print "Your email is " . $email. "<br>";

print " and your name is " . $name;

print ' and your comment is '. $comment;

?>