0% found this document useful (0 votes)
59 views

W1 PHP Tutorial

The document provides an introduction and overview of PHP including: - An agenda that introduces PHP and includes a demo and Q&A section - Examples of PHP scripts, variables, data types, operators, conditional statements, arrays, loops, functions, and handling GET requests - Recommendations for PHP resources like Zend Studio and the PHP manual website - Hints for how to evaluate expressions and use regular expressions in PHP projects
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
59 views

W1 PHP Tutorial

The document provides an introduction and overview of PHP including: - An agenda that introduces PHP and includes a demo and Q&A section - Examples of PHP scripts, variables, data types, operators, conditional statements, arrays, loops, functions, and handling GET requests - Recommendations for PHP resources like Zend Studio and the PHP manual website - Hints for how to evaluate expressions and use regular expressions in PHP projects
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 22

PHP Tutorial

Chu-Cheng Hsieh

Agenda

Demo of project 1A Introduce to PHP Q &A

Demo
Project 1A

Scripting block

<?php you PHP code here ?>

First example
<html> <body> <?php echo "Hello World";

/* Im comment */
?> </body> </html>
5

Variable in PHP

start with a $ sign symbol Loosely Typed Language


does not need to be declared start with a letter or an underscore "_ alpha-numeric characters and underscores (a-Z, 0-9, and _ ) not contain spaces

String

$txt="Hello World"; concatenation operator (.)

<?php $txt1="Hello World"; $txt2="1234"; echo $txt1 . " " . $txt2; ?>

Operators

Arithmetic

+, -, *, /, %, ++, -=, +=, -=, *=, /=, .=, %=

Assignment

Comparison

==, !=, >, <, >=, <=


&&, ||, !

Logical

Its time to introuce


Zend Studio http://www.zend.com/en/products/studio/

A useful resource

http://us2.php.net/manual/en/

10

ifelse

if (condition)

code to be executed if condition is true; code to be executed if condition is true;

elseif (condition)

else

code to be executed if condition is false;

11

example of if~else
<html> <body> <?php $d=date("D"); if ($d=="Fri") echo "Have a nice weekend!"; elseif ($d=="Sun") echo "Have a nice Sunday!"; else echo "Have a nice day!"; ?> </body> </html>
12

switch
<html> <body> <?php switch ($x) { case 1: echo "Number 1"; break; case 2: echo "Number 2"; break; case 3: echo "Number 3"; break; default: echo "No number between 1 and 3"; } ?> </body> </html>

13

Arrays

$names = array("Peter","Quagmire","Joe"); $names[0] = "Peter"; $names[1] = "Quagmire"; $names[2] = "Joe";

14

Associative Arrays

$ages = array("Peter"=>32, "Quagmire"=>30, "Joe"=>34); $ages['Peter'] = "32"; $ages['Quagmire'] = "30"; $ages['Joe'] = "34"; Example

<?php

$ages['Peter'] = "32"; $ages['Quagmire'] = "30"; $ages['Joe'] = "34"; echo "Peter is " . $ages['Peter'] . " years old.";

15

?>

Looping ~ while

<html>
<body> <?php
$i=1; while($i<=5) { echo "The number is " . $i . "<br />"; $i++; }

?> </body>

</html>
16

Looping ~ for

for (init; cond; incr) { code to be executed; } Example


<html> <body> <?php for ($i=1; $i<=5; $i++) { echo "Hello World!<br />"; } ?> </body> </html>
17

functions
<html> <body>

<?php
function writeMyName() { echo "Kai Jim Refsnes"; }

echo "Hello world!<br />"; echo "My name is "; writeMyName(); echo ".<br />That's right, "; writeMyName(); echo " is my name.";

?>
</body> </html>
18

How to handle GET


<html> <body> <form action="handle.php" method="GET"> Name: <input type="text" name="name" /> Age: <input type="text" name="age" /> <input type="submit" /> </form> <?php if($_GET["name"]){ echo "Welcome ".$_GET["name"]."<br />"; echo "You are ".$_GET["age"]." years old."; } ?> </body> </html>
19

Hint for project 1A


eval Example:
<html> <body>
<?php $equ = "3+4*5"; eval("\$ans = $equ ;"); echo "ans = ".$ans; ?> </body> </html>
20

Hint for Project 1A

preg_match
<html> <body> <? preg_match("/^(http:\/\/)?([^\/]+)/i", "http://www.php.net/index.html", $matches); print $host = $matches[0]."<BR>"; print $host = $matches[1]."<BR>"; print $host = $matches[2]."<BR>"; // get last two segments of host name preg_match("/[^\.\/]+\.[^\.\/]+$/","http://www.php.net",$matches); echo "domain name is: ".$matches[0]."\n"; ?> </body> </html>

21

matches

If matches is provided, then it is filled with the results of search.

$matches[0] will contain the text that matched the full pattern, $matches[1] will have the text that matched the first captured parenthesized subpattern, and so on.

22

You might also like