chrome hearts
asics

chrome hearts

chrome hearts
chrome hearts

Tuesday, January 7, 2014

Writing Your First PHP Script

Writing Your First PHP Script

I have always believed that the most fun way to learn a new programming language whether it is a language like C or a scripting language like PHP, is to use it to write a reallife useful program. Of course this is not the most systematic method of learning, but it works well if you already have some background in programming. This type of form is sometimes referred to as a FormMail or Form to Mail script. In later articles, I will probably develop that script and others to include features commonly found in such FormMail scripts.

I will have to assume that you have some knowledge of HTML code, otherwise this tutorial will wind up being tediously long. If you need help with HTML coding, you might try the beginners guide at:For those who are programmingsavvy, this is sort of like a Hello World program, but infinitely more useful!

WRITING THE FEEDBACK FORMThe first thing we need to do is to write the feedback form itself. This is made extremely easy by the facilities available in PHP. Do not put anything else into that file, ie, dont put in any other HTML tags or headers, etc. Likewise the contents of the message field is put into the variable $message.

All the script has to do is to call a special function appropriately called mail which would do the actual sending of the email. The first parameter to mail is actually the email address you want the form contents to be sent to, ie, your own email address. The second parameter is the Subject of the email message. The last two parameters are the content of the message and any other headers you want sent, respectively. We want a From header so that we know who is sending the email to us and can reply to him/her if we need to.

Notice that, like many other programming languages, strings sequences of characters are enclosed in double quotes, such as Feedback Form Results.

Variables like $message can be used asis.

You can also use single quotes like Hi there to quote strings, but when you do so, the variables included are not expanded.

Finally, it is appropriate to thank the visitor for his message. The header function allows us to send any HTTP header to the browser.

By the way, the script has to be enclosed within the tags because the PHP processor treats all input as HTML code unless otherwise specified. On some systems, you may need to use as the opening and closing tags to get the script to work.

Easy wasnt it? In just a few lines, youve written your first PHP script. And its not some trivial and useless script it is actually a working, usable program!

No comments:

Post a Comment