Sie sind auf Seite 1von 12

PHP Day 10

http://www.php.net http://www.mysql.com

Geshan Manandhar
Developer,
Young Innovations Private Limited
www.geshanmanandhar.com
GeshanManandhar.com 1
Email with PHP
<?php
$to = 'someone@somemail.com';
$subject = 'Test email';
$message = "Hello World!\n\nThis is my first mail.";
//define the headers we want passed. Note that they are separated with \r\n
$headers = "From: webmaster@example.com\r\nReply-To:
webmaster@example.com";
//send the email
$mail_sent = mail( $to, $subject, $message, $headers );
if($mail_sent){
print "Mail sent";
}
else {
print "Mail failed.";
}

?>
GeshanManandhar.com 2
Use of Emails
► Emails are used as an alert system in
database focused application.
► It is sent to notify application user about
various activities.
► Applications like e-card system rely heavily
on email.
► Server configuration, no. or emails allowed
etc are also vital in applications that use e-
mail.
GeshanManandhar.com 3
Back to basics
► Simple program to find a number is odd or even.
<?php
$number = 4;
if($number%2){
$odd = 1;
}
else {
$odd = 0;
}
//contd…
GeshanManandhar.com 4
Back to basics – C style printf
if($odd){
printf ("%d is odd.", $number);
}
else {
printf ("%d is even.", $number);
}
?>

GeshanManandhar.com 5
Back to the basics - Ternary
► Ternary operator
<?php
$number = 4;
$odd = ($number%2) ? 1 : 0; //ternary operator
//above single line is equivalent to 6 lines of code of previous program
if($odd){
printf ("%d is odd.", $number);
}
else {
printf ("%d is even.", $number);
}
?>
GeshanManandhar.com 6
Better programming mantras
► Use Ternary Operator to save time and LOC- Lines
of code.
► Utilize array and array related functions to the
maximum.
► Implement DRY in coding.
 Instead of creating multiple functions just add a
parameter to existing function to do the job.
► Use debugging techniques
 Use functions like print, echo, print_r, var_dump at
relevant breakpoints to check the value of the variable.

GeshanManandhar.com 7
If you have no web template
► Ifyou layout looks too dull and you can’t
design or don’t want to design.
► Then try out these websites to get free web
templates:
 http://www.opendesigns.org/
 http://www.freecsstemplates.org/
 http://www.freewebsitetemplates.com/
 More at
http://delicious.com/geshan/WebTemplates
GeshanManandhar.com 8
Simply theme your code
► Download the template you want.
► See the <div> structure.
► Change the static content to the dynamic
(PHP) content you want to replace with.
► Check your output.
► Hit and trail works if you are not good at
designing.

GeshanManandhar.com 9
A themed users table

GeshanManandhar.com 10
Questions

GeshanManandhar.com 11
To do
► Inject your code to a layout from above
mentioned website.
► Make your login system fully functional.
► Review you code – implement the mantras.
► Assign message to a variable $message as
per login information supplied by user, using
the ternary operator.
► Upload your files to your newly created sub-
domain at 000webhost, test them.
GeshanManandhar.com 12

Das könnte Ihnen auch gefallen