Download presentation
Presentation is loading. Please wait.
1
PHP & MySQL Introduction
2
At the end of this day you can work with
Goals At the end of this day you can work with Arrays Loops Includes And you’ll know about Basic syntax Conventions and names Default functions
3
Today: Integration and expansion
What did we do last week? What is PHP? Basic syntax Scripting & uploading A way of thinking Today: Integration and expansion 3
4
The classic Hello World
BASIC - PRINT "Hello world!" C - printf("Hello world\n"); Java System.out.println("Hello world!"); JavaScript document.write('Hello world!'); PHP - print "Hello World!"; Python - print("Hello World") Ruby - puts "Hello World!"
5
The classic Hello World
BASIC - PRINT "Hello world!" C - printf("Hello world\n"); Java System.out.println("Hello world!"); JavaScript document.write('Hello world!'); PHP - print "Hello World!"; Python - print("Hello World") Ruby - puts "Hello World!" 5
6
<p>I can see that, yes.</p>
HTML/PHP/HTML <html> <body> <h1>This is a heading</h1> <?php print “<p>This isn’t!</p>”; ?> <p>I can see that, yes.</p> </body> </html>
7
Variables Dynamic typing Case sensitive
Always start with a dollar sign Alphanumeric, can’t start with a number $actors = 12; $average_age = 70.8; $actor_of_the_week = “Sean Penn”; $is_dead = true; $actor_array[1] = “Johnny Depp”;
8
Arrays $colour_array[] = "blue"; $colour_array[] = "red"; $colour_array[] = "yellow"; print $colour_array[2]; $colour_array[2] = "green"; print_r( $colour_array );
9
Printing print $array[2]; print_r( $array );
Not a function but a language construct (like for, if, include, etc) Doesn’t need () to work Prints strings and numbers, not arrays print "<p>".$some_array."</p>"; results in html code "<p>array</p>" print_r( $array ); Is a function, needs () Prints all values of an array Best used within <pre> tags
10
Print_r() Array ( [0] => red [1] => blue [2] => Array
[0] => x [1] => y [2] => z )
11
2-dimensional array Array ( [0] => red [1] => blue
[0] => x [1] => y [2] => z ) 'red' 1 'blue' 2 'x' 'y' 'z'
12
Functions Can be put anywhere Case insensitive
Alphanumeric, can’t start with a number May conflict with default functions! $actor_array = GetActors(); function GetActors(){ //gets actors from database return db_array; }
13
This is just a textual thing! Variables
Coding conventions This is just a textual thing! General: make stuff descriptive Variables Lower case, underscores between words, suffix arrays with _array $time_of_day $date_array Functions Capitalised words, action first GetTimeOfDay()
14
Good action names for functions
IsCorrect( $question, $answer ); Checks something and returns true or false GetNameOfMonth( $date ); Does an operation and returns requested value SetActorToDead( $actor ); Changes a value, returns nothing MakeRow( $content_array ); Creates html with the content array and returns result
15
Loops & conditions for ( $i=0; $i<10; ++$i ){ if ( $i > 3 && $i < 7 ){ print $i; }
16
Loop with array $array[0] = 6; $array[1] = 4; $array[2] = 9; $array[3] = 5; $count = count( $array ); for ( $i=0; $i<$count; ++$i ){ print $array[$i]."<br>"; }
17
Download addsuptoten.zip from day 2 at the website
Exercise Download addsuptoten.zip from day 2 at the website Extract and open in editor There are instructions in the code Add your own code Compare the numbers in the array Which ones add up to ten? Upload and test regularily More testing = less thinking!
18
Loop with array $array['a'] = 6; $array['b'] = 4; $array['c'] = 9; $array['d'] = 5; //and now?
19
Loop with array $array['a'] = 6; $array['b'] = 4; $array['c'] = 9; $array['d'] = 5; //and now? for ( $i='a'; $i<'z'; ++$i ){ //won’t work }
20
Loop with array $array['a'] = 6; $array['b'] = 4; $array['c'] = 9; $array['d'] = 5; //use foreach foreach ( $array as $val ) { print $val."<br>\n"; }
21
While loops while ( $i < 10 ){ print $i."<br>";
$i += 1; //Danger! } reset( $array ); while ( list( $key, $val ) = each( $array ) ) { print $key."=".$val."<br>";
22
Made over the years, by many people
Default functions There are many of them Made over the years, by many people Not always consistent in naming
23
Naming inconsistencies
Array functions array_rand(); //random value from array shuffle(); //randomise array count(); //number of elements in an array String functions str_shuffle(); // randomise string strlen(); //length of string substr(); //keep part of string trim(); //clean up boundaries of string
24
Made over the years, by many people Most important libraries
Default functions There are many of them Made over the years, by many people Not always consistent in naming Most important libraries Strings, Arrays Math Date/Time Directories, Filesystem Session MySQL
25
Exercise See tasks for next week Default functions Loops
26
Always use the _once versions Language construct Mind the path
Includes Sooooooo handy! Always use the _once versions Basically: include_once Language construct No () needed Mind the path include_once “includes/head.inc.php”;
27
Download includes.zip from day 2 at the website
Exercise Download includes.zip from day 2 at the website Extract folder Open in index.php and includes in editor Add your own pages (say 3) Mind the title Add the menu Upload and test regularily More testing = less thinking!
28
At the end of this day you can work with
Goals At the end of this day you can work with Arrays Loops Includes And you’ll know about Basic syntax Conventions and names Default functions 28
29
Next week Server variables Forms Introduction to MySQL Homework: on the site
30
Done
Similar presentations
© 2025 SlidePlayer.com Inc.
All rights reserved.