Download presentation
Presentation is loading. Please wait.
Published bySpencer Patrick Modified over 9 years ago
1
Introduction to PHP Brendan Knight
2
What is PHP PHP is a general-purpose scripting language originally designed for web development to produce dynamic web pages. PHP code is embedded into the HTML source document or as a stand alone PHP is interpreted by a web server with a PHP processor module, which generates the web page document. PHP can collect data from forms, any form element will automatically available to PHP
3
PHP PHP is specified by And.php PHP supports many different kinds of databases including mysql and oracle PHP is used as the server-side programming language on over 25 million unique domains including Wikkipedia and Facebook.
4
Putting it in HTML <html><head> Example Example <body> <?php <?php echo "Hi, I'm a PHP script!"; ?> </body></html>
5
Form html Name: Age:
6
Sends to PHP Welcome.PHP Welcome ! Welcome ! You are years old. You are years old.
7
$_GET $_POST $_REQUEST Sends information Sends information All have particular features All have particular features Request covers both get and post Request covers both get and post
8
Variables All variables in PHP start with a $ sign symbol. All variables in PHP start with a $ sign symbol. Type is set automatically Type is set automatically Can be assigned Can be assigned Can be concatenated using the. operator Can be concatenated using the. operator
9
Examples <?php <?php $txt1="Hello World!”; $txt1="Hello World!”; $txt2="What a nice day!"; $txt2="What a nice day!"; echo $txt1. " ". $txt2; echo $txt1. " ". $txt2; ?> ?>
10
IF / assignment <html><body><?php$d=date("D"); if ($d=="Fri") echo "Have a nice weekend!"; else echo "Have a nice day!"; ?></body></html>
11
Other Conditionals Conditionals Arrays Arrays
12
Functions There are more than 700 built in functions There are more than 700 built in functions You can your own functions. You can your own functions. To keep the script from being executed when the page loads, you can put it into a function. To keep the script from being executed when the page loads, you can put it into a function. A function will be executed by a call to the function. A function will be executed by a call to the function. You may call a function from anywhere within a page. You may call a function from anywhere within a page. To let a function return a value, use the return statement. To let a function return a value, use the return statement.
13
Function example <?php <?php function writeName() function writeName() { echo ”Fred Nurk"; echo ”Fred Nurk"; } ?> ?> Somewhere “far far away” we call it <?php <?php echo "My name is ";writeName(); echo "My name is ";writeName(); ?> ?>
14
Handy to know Sessions Sessions Error handling Error handling Uploading files / file handling Uploading files / file handling e-mail e-mail Filtering for input type Filtering for input type Cookies Cookies Validating input Validating input
15
Data from Form.html to Insert.php Firstname: Firstname: Lastname: Lastname: Age: Age:
16
Insert.php connects Connect using <?php <?php $con = mysql_connect(servername,username,pas sword); $con = mysql_connect(servername,username,pas sword); if (!$con) { die('Could not connect: '. mysql_error()); } …………. if (!$con) { die('Could not connect: '. mysql_error()); } ………….
17
From Insert.php to db mysql_select_db("my_db", $con);$sql="INSERT INTO Persons (FirstName, LastName, Age) mysql_select_db("my_db", $con);$sql="INSERT INTO Persons (FirstName, LastName, Age) VALUES VALUES ('$_POST[firstname]','$_POST[lastname]','$_POST[age]')"; ('$_POST[firstname]','$_POST[lastname]','$_POST[age]')"; if (!mysql_query($sql,$con)) if (!mysql_query($sql,$con)) { die('Error: '. mysql_error()); die('Error: '. mysql_error()); } echo "1 record added"; echo "1 record added"; mysql_close($con mysql_close($con ?> ?>
18
Data from db The SELECT statement is used to select data from a database...SELECT column_name(s)FROM table_name The SELECT statement is used to select data from a database...SELECT column_name(s)FROM table_name http://www.w3schools.com/PHP/ http://www.w3schools.com/PHP/ http://www.w3schools.com/PHP/
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.