Basic php. Php Official site: PhP manual: Useful package (Apache, MySql,PhP):

13
Basic php

Transcript of Basic php. Php Official site: PhP manual: Useful package (Apache, MySql,PhP):

Page 1: Basic php. Php Official site:  PhP manual:  Useful package (Apache, MySql,PhP):

Basic php

Page 2: Basic php. Php Official site:  PhP manual:  Useful package (Apache, MySql,PhP):

Php

• Official site: www.php.net

• PhP manual: http://www.php.net/manual/en/index.php

• Useful package (Apache, MySql,PhP): http://www.easyphp.org/

Page 4: Basic php. Php Official site:  PhP manual:  Useful package (Apache, MySql,PhP):

What is PhP

• “PHP is a widely-used general-purpose scripting language that is especially suited for Web development and can be embedded into HTML” (www.php.net)

• Server scripting language (Javascript is a client side scripting language)

• If you know C, you will learn PhP in no time.

Page 5: Basic php. Php Official site:  PhP manual:  Useful package (Apache, MySql,PhP):

Hello World!• Index.php:

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

• Output<html>: <head> <title>PHP Test</title> </head> <body> <p>Hello World</p> </body>

</html>

Page 6: Basic php. Php Official site:  PhP manual:  Useful package (Apache, MySql,PhP):

Basics

• Use echo for printing• Variables start with $ sign• Do not have to explicitly assign them a

type (like int, array, etc.)• If $x=3 and $a=’x’, echo $$a will display 3,

because $$a=$($a)=$x=3;• . (dot) concatenates 2 strings. Ex

$a=”Hello”;$b=”World”; echo $a.” ”.$b; displays ”Hello World”

Page 7: Basic php. Php Official site:  PhP manual:  Useful package (Apache, MySql,PhP):

Accesing databases from PhP<html> <head>  <title>PHP Test</title> </head> <body><?php$link=mysql_connect("localhost",“username",“password") or die("Error connecting to the database server:".mysql_error()."<br>\n");mysql_select_db(“username") or

die("Error selecting the database:".mysql_error()."<br>\n");?>

<?php$sql = "SELECT fname, sid FROM students;";

$result=mysql_query($sql);while(list($name,$id) = mysql_fetch_array($result)){

echo "<p>".$name." ".$id;}?></body></html>

Page 8: Basic php. Php Official site:  PhP manual:  Useful package (Apache, MySql,PhP):

Arrays

• A mapping between keys and values. Example

<?php$arr = array("foo" => "bar", 12 => true);echo $arr["foo"]; // barecho $arr[12];    // 1

foreach ($arr as $key => $value) {

echo “key=”.$key;echo “ value=”,$value.”<BR>”;

}

?>

Page 9: Basic php. Php Official site:  PhP manual:  Useful package (Apache, MySql,PhP):

Get and Post

• $_GET[’variablename’] – the variable is appended to the http address, and is transmited in the same stream. Ex: http://www.somesite.com/index.php?a=1&b=’x’. Easy to bookmark

• $_POST[’variablename’] – a new stream is created, and all the post variables are transmited in this stresm. The http address is clean (no bookmarks)

Page 10: Basic php. Php Official site:  PhP manual:  Useful package (Apache, MySql,PhP):

Get variables from formsgetName.html<HTML><BODY><form action="action.php"

method="post"> <p>Your name: <input type="text"

name="name" /></p> <p>Your age: <input type="text"

name="age" /></p> <p><input type="submit"

value="Submit"/></p> </form> </BODY></HTML>

Action.php<HTML><BODY><?php echo "Your name is ".$_POST["name"]." and you are ".$_POST["age"]." years old";?></BODY></HTML>

Exercise: try it with “get” now.

Page 11: Basic php. Php Official site:  PhP manual:  Useful package (Apache, MySql,PhP):

Some usefull global variables

• $_SERVER[’PHP_SELF’]- The filename of the currently executing script, relative to the document root. For instance, $_SERVER['PHP_SELF'] in a script at the address http://example.com/test.php/foo.bar would be /test.php/foo.bar.

• $_SERVER['SERVER_NAME']- The name of the server host under which the current script is executing. If the script is running on a virtual host, this will be the value defined for that virtual host.

• $_SESSION -An associative array containing session variables available to the current script.

• __LINE__The current line number of the file.

• __FILE__The full path and filename of the file. If used inside an include, the name of the included file is returned.

Page 12: Basic php. Php Official site:  PhP manual:  Useful package (Apache, MySql,PhP):

Session

• HTTP is stateless

• Simulates a statefull connection. Maintains records on the server side until the browser is closed.

• session_start();

• $_SESSION[‘foo’]=‘bar’;

• $_SESSION[‘a’][‘b’]=‘c’;

• Allows to build a ”shoping cart”;

Page 13: Basic php. Php Official site:  PhP manual:  Useful package (Apache, MySql,PhP):

Includes• header.php• <html>•  <head>•   <title>PHP Test</title>•  </head>•  <body>• <TABLE width="800" cellpadding="4" cellspacing="0" align="center" border="0">• <TR>• <TD width='100%'>

• footer.php• </TD>• </TR>• </TABLE>• </body>• </html>

• index.php• <?php• include ”header.php”;• echo ”hello world”;• include ”footer.php”• ?>