Lab 2 review PHP intro MVC (Model View controller) What is PHP? • Är ett allmänt open source server-side skriptspråk som ursprungligen utformats för webbutveckling • Syfte att producera dynamiska webbsidor • PHP-koden bäddas ofta in i HTML • Källdokumentet, php sidan (filen), tolkas av en webbserver och dess installerade php motor (Zend Engine) • Som efter tolkning (parsing), genererar och skickar innehållet till klienten PHP how? Execution of PHP?
• Diagrammet (förra sliden) visar arkitekturen och grundläggande dataflödet för PHP-baserade webbsidor. • Browser initierar ett förfrågan av en php sida till webbservern, som skickar en begäran vidare till PHP Web Server Interface (1). • Web Server Interfacet anropar Zend Engine (2), som har åtkomst till webserverns hårddisk. Från hårddisken hämtas koden PHP-sidan och skickar PHP-kod till Run-time kompilator (3). • Zend Engines Run-time-kompilator skapar en komplierad version av scriptet, som skickas vidare till Zend Engine Executor (4). Executor genererar HTML som kan ses av webbläsaren. • Om script behöver tillgång till andra moduler, såsom SQL-databas, XML etc, skickar Zend Engine den koden vidare lämplig PHP- moduler, som processer begäran, och skickar resultatet till webbserverns Interface (5). • Som sedan skickar HTML till webbläsaren (6). PHP - www.php.net
• PHP (Personal Home Page) or PHP Hypertext Processor is a server-side script language - you must have a running web server in order to try it out! • Many Web CMS systems are based on PHP • PHP code which can be mixed with HTML exist in .php files • Remeber the client browser will NOT be able to se the code! • It does not matter if you use PHP or HTML mode – a PHP file however always start off in HTML mode • To insert CR (Carriage Return) and LF (Line Feed) put “\r\n”
echo "";
echo ""; "; ?> echo ""; ?> Where? School and PHP help • I en fil med filändelsen .php, tex. shopping.php – Oftast inbäddat i HTML koden • Med hjälp av editor eller plugin – Notepad++, Dreamweaver, Codelobster, …, Eclipse – To install latest stable PDT build in Eclipse, open "Help -> Install New Software" and enter following URL: http://download.eclipse.org/tools/pdt/updates/release • Skolans webbserver har stöd för php → phpinfo();– http://users.du.se/~hjo/cs/dt1040/phptest.php – PHP v5.3.x – 5.5.x
$message = "This is my Web server, "; $message .= date("H:i"); } $m = mail($to, $subject, $message); ?> echo "Return code from mail was " . $m; exit(); ?> PHP basics – operators PHP basics • Variable scope – Each variable has an area in which it exists, known as its scope – Any variables not set inside a function or an object are considered global • To define strings use either ' or ” as 'Hans' or ”Hans” • Constants – define(name, value); • Pre set constants as
• Checking if a variable is set $foo = 1; if (isset($foo)) { – The isset() function echo "Foo is set\n"; } else { • Functions provide the core of echo "Foo is not set\n"; PHP's programming functionality } if (isset($bar)) { – Strings, Date and Time, echo "Bar is set\n"; } else { Mathematics, Regular expressions echo "Bar is not set\n"; – User functions } ?> PHP basics – variables and echo
• echo vs. print? – print cannot chain text output with the comma syntax – print returns a value PHP loops and comments
• The for and while loops works excactly as in other C derived or influenced languages as Java, C# etc. • To comment your code in a php snippet use: #, /*…*/ or //
echo ""; My web server still knows its 7 times table echo "";
echo "
"; # a while loop while($i <= 10) # a for loop { for ($x = 1; $x <= 10; $x++) echo $i; { echo " times 7 is "; echo $x . " times 7 is " . $product = $i * 7; $x * 7 . "
"; echo $product . "
"; } $i = $i + 1; echo ""; } echo ""; ?> ?>