html head titleExampletitle head body php echo Hi
Παράδειγμα <html> <head> <title>Example</title> </head> <body> <? php echo "Hi, I'm a PHP script!"; ? > </body> </html>
If statement Η δομή μιας εντολής if ειναι ως εξής: IF (something == something else){ THEN Statement } else { ELSE Statement }
If statement Παράδειγμα: � � � if ($username == "webmaster") { echo "Please enter your password below"; } else { echo "We are sorry but you are not a recognised user"; } if ($name == "" || $email == "" || $password == "") { echo "Please fill in all the fields"; }
While loop Παράδειγμα $number = 1000; $current = 0; while ($current < $number) { ++$current; echo "$current "; }
Πίνακες Παράδειγμα: $names[0] $names[1] $names[2] $names[3] $names[4] = = = 'John'; 'Paul'; 'Steven'; 'George'; 'David';
Χρήση πινάκων και loop stats Παράδειγμα: $number = 5; $x = 0; while ($x < $number) { $namenumber = $x + 1; echo "Name $namenumber is $names[$x] "; ++$x; }
Ε-mail με ΡΗΡ Παράδειγμα: $to = “yourmail@yourmail. gr"; $subject = "PHP Is Great"; $body = "PHP is one of the best scripting languages around"; $headers = "From: webmaster@webmaster. grn"; mail($to, $subject, $body, $headers); echo "Mail sent to $to";
Φόρμες με ΡΗΡ Παράδειγμα: <form action="mail. php" method="post"> Your Name: <input type="text" name="name"> E-mail: <input type="text" name = "email"> Comments <textarea name="comments"></textarea> <input type="submit" value="Submit"> </form> Τώρα πρέπει να φτιάξουμε το αρχείο mail. php
Φόρμες με ΡΗΡ Mail. php : <? function check. OK($field) { if (eregi("r", $field) || eregi("n", $field)){ die("Invalid Input!"); } } $name=$_POST['name']; check. OK($name); $email=$_POST['email']; check. OK($email); $comments=$_POST['comments']; check. OK($comments); $to="yourmail@yourmail. gr"; $message="$name just filled in your comments form. They said: n$commentsnn. Their e-mail address was: $email"; if(mail($to, "Comments From Your Site", $message, "From: $emailn")) { echo "Thanks for your comments. "; } else { echo "There was a problem sending the mail. Please check that you filled in the form correctly. "; } ? >
SQL με ΡΗΡ Παράδειγμα: <? php $dbhost = 'localhost'; $dbuser = 'root'; $dbpass = 'password'; $conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql'); $dbname = 'uoidb'; mysql_select_db($dbname); //. . . do something like insert or select, etc mysql_close($conn); // …close connection ? >
- Slides: 19