Friday, April 22, 2011

Loop the Loop

Every programming language has a number of methods for creating loops, and PHP is no exception. Try this program, for example, either by uploading it the server and seeing what happens, or just by reading the code and trying to work it out:<?phpecho "<HTML>";echo "<body>";echo "A web server that knows its 7 times table!<br><br>";for ($x = 1; $x <= 10; $x++){echo $x . " times 7 is " . $x * 7 . "<br>";}echo "</body>";echo "</HTML>";?>Run the program, and you'll see the following in your web browser: One line of PHP that you'll not have seen before is:for ($x = 1; $x <= 10; $x++)This is...

Passing Information to PHP

There are various ways of passing information to a PHP program, and we’ll cover many of them in the database-related chapters that follow, and when we come to talk about HTML forms later on. But here’s a fun example for now, which illustrates a couple of important points about PHP. Open PSPad, delete the contents of demo.php and replace it with the following:<?php$person = $_GET["name"];echo "<HTML>";echo "Hello ";echo $person;echo ", how are you today?";echo "</HTML>";?>Surf to the demo.php page and you’ll see a message that says "Hello , how are you today". Here, we’re passing 2 variables called firstname and surname....

Tuesday, April 19, 2011

Sending Email with PHP

With server-side programming you can do just about anything, and that includes sending email. Try this little PHP program, by changing the contents of demo.php to the following and then surfing to the demo.php page:<?php$to = "robert@the-web-book.com";$subject = "Hello";$message = "This is my Web server, sending me email on ";$message .= date("D");$message .= " at ";$message .= date("H:i");$m = mail($to,$subject,$message);echo "Return code from mail was " . $m;exit(); ?>You should see a message which says "Return code from mail was 1". To understand why, and what this program does, let’s examine it in detail.The first line of code creates a new variable called $to, which is set to the "to" address of the email we’re going to be sending. In this case, I’m sending the message to myself....

Random numbers in PHP

Do you think that I log in every day to update those figures and dates? Nope. Everything is done automatically. There’s a PHP function which returns the date that a file was last changed. So all I have to do is upload a new PDF file each time I make changes to the book.A couple of lines of PHP code then produces the “Last updated…” section of the web page, automatically looking up and formatting the file’s date. Even the “NEW” marker is automatic. If the date of the PDF file is within 7 days of today’s date, the marker gets displayed. Otherwise, it doesn’t. So I never have to remember to remove the marker, because the web site does it for me.As...

Pages 81234 »
 
Design by Free WordPress Themes | Bloggerized by Lasantha - Premium Blogger Themes | Grants For Single Moms