PHP Day 10

Geshan Manandhar Developer, Young Innovations Private Limited 1

Email with PHP
<?php $to = ''; $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:\r\nReply-To:"; //send the email $mail_sent = mail( $to, $subject, $message, $headers ); if($mail_sent){ print "Mail sent"; } else { print "Mail failed."; } ?> 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. 3

Back to basics
► Simple

even. <?php $number = 4; if($number%2){ $odd = 1; } else { $odd = 0; } //contd…

program to find a number is odd or


Back to basics – C style printf
if($odd){ printf ("%d is odd.", $number); } else { printf ("%d is even.", $number); } ?> 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


if($odd){ printf ("%d is odd.", $number); } else { printf ("%d is even.", $number); } ?> 6

Better programming mantras
► Use

Ternary Operator to save time and LOCLines 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. 7

If you have no web template
► If

you 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:
    More at 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. 9

A themed users table




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.


Sign up to vote on this title
UsefulNot useful