PHP : What is it?
PHP stands for PHP Hypertext Preprocessor. "PHP is an HTML-embedded scripting language. Much of its syntax is borrowed from C, Java and Perl with a couple of unique PHP-specific features thrown in. The goal of the language is to allow web developers to write dynamically generated pages quickly."
When someone visits your PHP web page, your web server processes the PHP code. It then sees which parts it needs to show to visitors (content and pictures) and hides the other stuff (file operations, math calculations, etc.) then translates your PHP into HTML. After the translation into HTML, it sends the web page to your visitor's web browser.
In Brief
PHP stands for PHP: Hypertext Preprocessor
PHP is a server-side scripting language, like ASP
PHP scripts are executed on the server
PHP supports many databases (MySQL, Informix, Oracle, Sybase, Solid, PostgreSQL, Generic ODBC, etc.)
PHP is an open source software (OSS)
PHP is free to download and use
MYSQL : What is it?
MySQL is a database server
MySQL is ideal for both small and large applications
MySQL supports standard SQL
MySQL compiles on a number of platforms
MySQL is free to download and use
Combination of PHP with MySQL
PHP combined with MySQL are cross-platform (means that you can develop in windows and serve on a Unix platform)
Why PHP? PHP runs on different platforms (Windows, Linux, Unix, etc.)
PHP is compatible with almost all servers used today (Apache, IIS, etc.)
PHP is FREE to download from the official PHP resource: www.php.net
PHP is easy to learn and runs efficiently on the server side
PHP : What it does? It is also helpful to think of PHP in terms of what it can do for you. PHP will allow you to:
Reduce the time to create large websites.
Create a customized user experience for visitors based on information that you have gathered from them.
Open up thousands of possibilities for online tools.
Allow creation of shopping carts for e-commerce websites.
What You Should Know? Before starting this tutorial it is important that you have a basic understanding and experience in the following:
• HTML - Know the syntax and especially HTML Forms.
• Basic programming knowledge (Preferably C) - Traditional programming experience it will make learning PHP a great deal easier.
Introduction
Until recently, the script is something on the Internet, only a few people even attempted, let alone master. Recently, however, an increasing number of people have set up their own websites and scripting languages have become more important. Because of this, scripting languages are becoming easy to learn, and PHP is the easiest, most powerful yet.
What is PHP?
PHP representative Hypertext Preprocessor is a server-side language. This means that scripts running on the Web server, rather than the user's browser, so you do not have to worry about compatibility issues. PHP is relatively new (as opposed to languages such as Perl (CGI) and Java), but is fast becoming one of the most popular scripting language on the Internet.
Why is PHP's?
You may be wondering why you should choose PHP and other languages such as Perl, or even why you want to learn a scripting language. I will be the first scripting language to learn. To learn a scripting language, or even understand, can open up significant new possibilities for your site. Although you can download pre-made scripts from sites, such as Hotscripts, they usually contain advertising the author or will not be what you want. With the understanding of scripting languages, you can easily edit these scripts to do what you want, or even create their own scripts.
Using a script on the website can add many new 'interactive' features, such as feedback forms, guestbooks, message boards, counters and more advanced features, such as portal, content management, advertising management, etc. With these kind of things Your site will find that it provides a more professional image. Not only that, anyone who wants to work in the development of the industry Web site will find it easier to find work if they know a scripting language.
What do I need?
As mentioned previously, PHP is a server-side scripting language. This means that although your users do not need to install new software, virtual host will need to have PHP set to their server. It should be included as part of the package, but if you do not know if this is you can find a script to install the first use of this tutorial. If your server does not support PHP you can ask your web host to install it, because you can free download and installation. If you need a low-cost virtual hosting support PHP, I recommmend HostRocket.
Write PHP
Write PHP on your computer is quite simple. You do not need any dedicated software services, in addition to a text editor (such as Notepad on Windows). Run this point, you can write your first PHP script.
PHP's statement
PHP scripts are always included in the two PHP tags. This tells your server to parse the information between them as PHP. Three different forms are as follows:
"?
PHP code in here
? "
"? PHP's
PHP code in here
PHP's? "
<script language="php">
PHP code in here
"/ Script"
All of these work exactly the same, but in this tutorial, I will use the first option ( "? And?"). No particular reason for this, though, you can use the option. You have to remember that, although the same start and end code tags (can not start "? And the end of the" / script "for example).
Your first script
The first PHP script, it will be a very basic writing. All need to do is to print out all of the information on the PHP server. Type the following code into a text editor:
"?
phpinfo () function;
? "
You can see that this is only one line of code. This is a standard PHP function call phpinfo This will tell the server to print out a standard form to your information, set up servers.
Another thing you should note that in this case is the first
The semicolon at the end of the line. This is very important. Unlike many other scripting and programming languages, almost all the lines of the final semicolon, if you miss you will get an error.
Finishing and test scripts
Now that you have completed your script save it as phpinfo.php, and upload to your server and the normal way. Now, use the browser is taking the URL script. If you have a job (if PHP is installed on your server), you should get a huge page full of information about PHP on the server.
If your script does not work, and displays a blank page, you either spelling error code or your server does not support this feature (although I have not found a server that does not). On the contrary, if a page is displayed that prompts you to download the file, PHP is not installed on your server, you should be able to retrieve the algorithm is a new virtual host or ask your current host to install PHP.
This is a good idea to keep this script for future reference
Print version
Output text in your PHP script is quite simple. Unlike most other things in PHP, you can do this in a variety of different ways. The main one you will use it is printed. Will allow you to print out the text, variables or combinations of two, so that they displayed on the screen.
Print statement for the following ways:
Print ( "Hello world!");
I will explain the above line:
Printing is a command, and tell the script what to do. Second, the information printed out, which is included in parentheses. Because you are the output text, which also was surrounded instide quotation marks. Finally, almost every line of a PHP script, it must end with a semicolon. You will, of course, must be accompanied by the standard in your PHP tags, so the following code:
"?
Print ( "Hello world!");
? "
This will show:
Hello world!
On the screen.
Variable
With other programming languages, PHP allows you to define variables. There are several variables in PHP type, but the most common is a String. It can hold text and numbers. All of the $ symbol beginning of the string. To specify the number of text strings you can use the following code:
$ Welcome_text = "Hello, welcome to my website.";
This is a very simple line to understand, everything inside the quotation marks will be assigned to the string. You have to remember that some rules, but the string:
String is such a case sensetive $ Welcome_Text is not the same as $ welcome_text
String name can contain letters, numbers and underscores, but not a number or underscore
When the distribution figures for the strings you do not have this offer include:
$ User_ID's = 987
Will be allowed.
Output variables
To display the variable on the screen using the same code to display the text, but slightly different forms. The following code will show you a welcome text:
"?
$ Welcome_text = "Hello, welcome to my website.";
Print ($ welcome_text);
? "
As you can see, the only major difference is that you do not need quotation marks if you want to print a variable.
Formatted text
Unfortunately, the output of your PHP program is really boring. Everything is just the output in the browser's default font. It is very easy, however, formatted text using HTML. This is because, PHP is a server-side language, code execution before the page sent to the browser. This means that only the result of information sent from the script, so the above example, the browser will only send the text:
Hello, Welcome to my website.
This means that, though, you can include a standard HTML tags in your script and strings. The only problem is that many HTML tags need to "sign. You may notice, which would conflict with the quotation marks is used to print text. This means that you have the script should be in quotation marks (those at the beginning and end of the output), Which should be ignored (those in the HTML code).
In this example, I change the text to Arial font is red. This will be the normal code:
<font face="Arial" color="#FF0000">
"/ Fonts"
You can see the code contains four quotes that will confuse the script. Therefore, you must add a backslash before each quotation mark, so PHP script to ignore it. The code would chang
E to:
<font face=\"Arial\" color=\"#FF0000\">
"/ Fonts"
Now, you can include in your print statement:
Printing (the "<font face=\"Arial\" color\"#FF0000\"> Hello, welcome to my website." / Fonts "");
This will make the browser displays:
Hello, Welcome to my website.
Because it is only to be sent the code:
<font face="Arial" color="#FF0000"> Hello, welcome to my website. "/ Fonts"
This is not difficult to output HTML code in the browser, but later in this tutorial, I'll tell you another way to do this, you can make it easier.
Basic knowledge of IF
If the statement is used to compare two values, to perform different actions based on test results. If the statement takes the form So, if another. Basically, IF part of the inspection conditions. If this is true, then statements. If not, or else statement.
IF Strucure
The structure of the IF statement is as follows:
IF (something else ==)
(
Then the statement
Otherwise, ()
ELSE statement
)
Variable
The most common comparison used in IF statement is a variable to the other pieces of text, numbers, or other variables. For example:
If ($ user == "Administrator")
This will compare the contents of the variable text strings. Was part of the code are performed only if the variables are exactly the same content, quotation marks, if the variable contains the 'administrator' or 'WEB site administrator' This would be a mistake.
The statement at the time to build
Add to your script, you can now add a statement:
If ($ user == "admin") (
Echo "Please enter your password below";
)
This will only display the text, if the user name is administrator. If not, what will be displayed. In fact, you can leave such an IF statement, because there is no practical need, there is one other part of it. This is especially useful if you use multiple IF statements.
Construction Else statement
Add ELSE statement is very simple, what was said. Just to add some extra code:
If ($ user == "admin") (
Echo "Please enter your password below";
Otherwise, ()
Echo: "I am sorry, but you do not approve of the user";
)
Of course, you can not just be limited to line of code. You can add any PHP commands in between the braces. You can even include other IF statments (nested statements).
Other more
There are other ways you can use the IF statement to compare values. First of all, you can compare two different variables to see if their values match, for example,
If ($ enteredpass == $ password)
You can also use the standard comparison symbols to check if a variable is greater than or less than another question:
If ($ age "" 13 ")
Or:
If ($ date "$ end)
You can also check the number of tests, in an IF statement. For example, if you have a table, you want to check if any field blank, you can use:
If ($ name ==""| | $ e-mail ==""| | $ password =="")(
Echo "Please fill in all fields";
)
WHILE loop
while loop one of the most useful commands in PHP. It is also very easy to set up and use. A WHILE loop will be, by definition, the implementation of a code, until the conditions are met.
Repeat a set number of times
If you have a piece of code, you have to repeat several times without re-typing it, you can use the while loop. For example, if you want to print out the words "Hello World" 5 times, you can use the following code:
$ Times = 5;
$ X = 0;
While ($ x "$-fold) (
Echo "Hello World";
+ + $ X;
)
I will now explain this code. The first two lines, just to set variables. U.S. dollars saved in a variable number of times to repeat the code. In the $ x variable is a calculation of how many times the code has been executed. After these are deeds. This tells the computer to repeat the code while $ i is less than $ times (or repeat until $ i is equal to $ times). This is the second to implement the code is included in ().
After the echo line, print out the text, there is a very important line:
+ + $ X;
Do here is exactly the same text:
$ X = $ x + l of the;
It increases the value of a ten-dollar this code, and then repeat (eg, $ X is equal to 1). It continues to be repeated until $ x is equal to 5 (the value of times), the computer will then move to the next part of the code.
The use of $ x
Repeat the calculation of the number of variables ($ x in the above example) can be used not only the calculation. For example, if you want to create a web page with all the numbers, from 1 to 1000, you can either type out every one or you can use the following code:
$ Number = 1000;
$ Current = 0;
While ($ now "$ number) (
+ + $ Current;
Echo "$ current <br>";
)
There are several things to pay attention to this code. First, you will find that I have already placed + + $ current; in the echo statement. This is because, if I do not, it will start printing numbers from 0, which is not what we want. In the + + $ current; line can be placed anywhere in the WHILE loop, it does not matter. Of course it can, add, subtract, multiply, divide or do any other number, as well as.
Another reason is that if + + $ current; lines after the echo line will stop the loop, we found the number 999 because it would check $ current, equivalent to 1000 (as contained in last year's Central) and will stop, even though 1000 has not yet printed.
Array
Array is common in many programming languages. They are special variables that can hold multiple values, each number is stored in its own 'space' in the array. Array is very useful, especially in the use of while loop.
The establishment of an array
The establishment of an array of slightly different, the establishment of normal variables. In this example, I will set up an array with 5 are:
$ Name [0] = 'John';
$ Name [1] = 'Paul';
$ Name [2] = 'Steven';
$ Name [3] = 'George';
$ Name [4] = 'David';
As you can see, some of the array are numbers starting from 0. To add an array of values must be specified in the location of the array, through the a number of [].
Reading from an array
Reading from an array is to the same information that only you have to do is put the number of the array and the data array. So, if I want to print out the third name, I can use the code:
ñ
Echo "The third name is $ names [2]";
This will output:
The third name is Stephen
The use of arrays and loops
One of the best use of a circular array of information in the output. For example, if I want to print out the names of the following list:
Name 1 John
Name 2 is Paul
Name 3 Stephen
Name of 4 George
5 David
I can use the following code:
$ Number = 5;
$ X = 0;
While ($ x "$ number) (
$ Namenumber = $ x + l of the;
Echo "name $ namenumber is $ names [U.S. dollars] <br>";
+ + $ X;
)
As you can see, I can use the variable $ x from my loop to print an array of well-known. You may have noticed that I also use the variable $ namenumber it is always a greater than $ x for this is because arrays start from 0 number, the so-called number of the correct name of the output I have to add a real value.
The message command
E-mail is very easy to send from PHP, using a different scripting language, need special settings (such as CGI). In fact, there is only one command, mail () send the message. It is used as follows:
E-mail ($ to, $ subject, $ body, $ head);
In this example, I use a variable, because they are descriptive name, but you can only occur in the message text in the command. First of all, $ to. This variable (or command) contains the e-mail address e-mail will be sent. $ Theme is the subject of an e-mail and $ sector is the actual text of the e-mail.
In this section $ headers used for any other e-mail headers you may need to add. The most common use, this is an e-mail from the field, but you can also include other headers, such as CC, and confidential.
Email
In sending e-mail, if you use a variable, you must, of course, set up in advance the contents of the variable. Here are some simple code to send a message:
$ To = "php@gowansnet.com";
$ Subject = "PHP is a big";
$ Body = "PHP is the best scripting languages around";
$ Header = "person: webmaster@gowansnet.com \ N" of the;
E-mail ($ to, $ subject, $ body, $ head);
Echo "Mail sent to $ to";
This code will acutally do two things. First of all, it will send a message to the php@gowansnet.com theme 'PHP is a big' and text:
PHP is the best scripting language around
And e-mail from the webmaster@gowansnet.com. It will also output text:
E-mail sent to php@gowansnet.com
To the browser.
Format e-mail
Is that you may have noticed that the example is from the line end of the \ n. This is acutally a very important character, send an e-mail. This is a new line character and tells PHP to take a new course, e-mail. This is very important, which is placed in the first, each add, so your e-mail will be in accordance with international standards, will be delivered.
Into the \ n code can also be used in the body's Day e-mail to wrap, but can not be used in the subject or the addressee field.
E-mail is not a variable
E-mail the above may have been sent to a different variable names (this is the location of variables and a comma, rather than their name is to determine its use). It may also have been made to use an online text is as follows:
E-mail ( "php@gowansnet.com", "PHP is great", "PHP is the best scripting languages around", "From: webmaster@gowansnet.com \ N" in);
However, this will enable you to code a little difficult to read.
Error Control
As for the people who already have a script for some time know that it is very error-prone code, but also very easy to enter a valid e-mail address (especially if you are using a script form of mail). Because of this, you can add a small piece of code will check the e-mail:
If (mail ($ to, $ subject, $ body, $ headers)) (
Echo "a message is sent to the $ and subject: $ subject";
Otherwise, ()
Echo "There is a problem, send e-mail. Check your code, and ensure that the e-mail address $ to is valid";
)
This code is a sense of self explanitory. If the message is sent successfully, will output a message telling the user's browser, if not, it will display an error message of some of the recommendations to correct the problem.
Set your table
The set-up using the same PHP script normal HTML. Because this is a PHP tutorial, I will not enter the depth of how to write the form, but I will tell you the three main parts of the code, you must know:
<input type="text" name="thebox" value="Your Name">
Will display a text input box with your name written on it by default. The value of some of this code is optional. This information will be the name of the definition of the name text box, it should be unique.
<textarea name="message">
Please fill in your message.
"/ Textarea's"
Will display a large scrolling text box text 'Please write down your message here. As the default. Similarly, the name of the definition should be unique.
<input type="submit" value="Submit">
This will create a button to submit the form. You can change it on the button that by changing the value of the button.
All the elements for your form must be included in the <form> tag. They are used as follows:
<form action="process.php" method="post">
Form elements and format
"/ Form"
In the form of action to tell it what script to send data (in this case, it process.php). This also can be a complete URL (for example, http://www.mysite.com/scripts/private/processors/process.php). This method tells the form how to submit their data. Post data to send the data stream script, the request. Get is another option. GET will send the form data in the form of the URL so that after the emergence of a question mark, for example, http://www.mysite.com / process.php? Name = david
This is really no difference between the system uses, but is usually best to use the Post Office if you use a password or sensitive information, because they should not be displayed in the browser address bar.
Access to information in the form of
The next step is to obtain the data forms have been submitted to your script, you can use it to do something. It is. There are basically two different ways to obtain data to PHP, this depends on how they are made. There are two ways to submit, GET and POST, which can be used simultaneously in the form. The difference between the two is to use GET, variables and data will be displayed in the page's address, but using POST it is invisible. GET benefits, but you can submit information, there is no script in the form of, by simply editing the URL.
This applies to the use of the same form GET. The advantage is that you can create a link to your script, it does different things depending on click on the link. For example, you can create a script that will display a different page under the link to click-through rate:
yourpage.php? User = David
David's web page can be displayed, and:
yourpage.php? User = Tom
Tom can display the page, use the same script.
The above information can also be a script that will use the system by separating them in & symbols:
yourpage.php? User = David & referrer = gowansnet & area = 6
All of these can be used to access the GET variable users, referral and regions.
To obtain a variable has been sent to the script using the POST method use the following code:
$ Variablename = $ Hikohiro [ 'variable'];
Basically a variable POST (the name of the form fields) and assign it to the variable $ variablename.
Similarly, if you are using a GET method, you should use the form:
$ Variablename = $ _GET [ 'variable'];
This should be done for each variable you want to use your form (or URL).
E-mail the script to create the form
To complete the closure of this section, I will tell you what you have learned how to use this part of the final establishment of a system, e-mail user's comments to you.
First of all, create this table for your HTML page:
<form action="mail.php" method="post">
Your name: <input type="text" name="name"> <br>
E-mail Name: <input type="text"名稱= "email"> <BR> <BR> in the
Comments <br>
<textarea name="comments"> "/ textarea's" <BR> <BR> in
<input type="submit" value="Submit">
"/ Form"
This will enable a simple form, users can enter their e-mail address, their names and their views. You can, of course, add an extra part, but remember that this form of the updated script. Now create a PHP script:
"?
Functional checkOK ($ field)
(
If (eregi ( "\ r", that is, $ field) | | eregi ( "\ N", and $ field)) (
Die ( "Invalid Input!");
)
)
$ Name = $ Hikohiro [ 'name'];
checkOK ($ name);
$ Email = $ Hikohiro [ 'email'];
checkOK ($ email);
$ Comment = $ Hikohiro [ 'comments'];
checkOK ($ comment);
$ To = "php@gowansnet.com";
$ Message = "$'s name, only your comments fill out a form. They said: \ ñ $ comment \ ñ \ nTheir e - mail address is: $ email";
If (mail ($ to, "comments from your website", $ message, "From: $ email \ ñ")) (
Echo "Thank you for your advice.";
Otherwise, ()
Echo "There is a problem, send e-mail. Please check your fill in the form correctly.";
)
? "
Please keep in mind to replace the php@gowansnet.com use their own email address. The script should be saved as mail.php, should be set. Now, all you need to do is fill in your comment form.
The first part is the script may look somewhat strange:
Functional checkOK ($ field)
(
If (eregi ( "\ r", that is, $ field) | | eregi ( "\ N", and $ field)) (
Die ( "Invalid Input!");
)
)
You do not need to worry about, this is what to do, but basically, it is to stop using your junk e-mail form to send spam by checking the input special characters do not exist and can be used to send messages to trick the computer into the other address. This is a role of the analysis is the examination of these characters, if they are found, stop running the script.
The line:
checkOK ($ name);
So to run this check for each input, to ensure that it is valid.
Comments
As with any programming language, which is very important comment in your script. If you are a script working with others, you have to let them know your code, if not you distribute your script, you need to show people how to edit it. Even if you are the only who is using your script is useful advice so that you can edit it at a later date.
In PHP there are two ways you can comment. One approach is to use single-line comment, the other one is mainly used for comments, to go above the. Single-line comment is written as follows:
/ / You can comment here
Everything after the / / will be ingnored script execution. You can even end in these places to another line, such as the
Print "Hello $ name"; / / Welcome user
Another way is to use multi-line comments Note:
/ * The following piece of code will take into
To the user, and check whether it is valid before
It added to the database * /
Any between / * and * / will be ignored. Important thing is, you can always maintain this type of comment is not doing so can make your script not work.
Print, echo, and HTML
As you may have noticed, in this tutorial, I actually used 4 different ways to output to the browser's information:
Echo ( "text here");
Echo "Text here";
Print ( "text here";
Print "Text here";
In order to clarify all these do the same thing, you can use any or all of these in the script. There is no reason, or even use the same type of all through the script. The only problem, you may find that, as I said in Part 2, all "in HTML code must be replaced with \", if you have a lot of code, it may take a long time. This makes me a very useful role in PHP. If, for example, you create the title page of the dynamics in PHP, the then, static page, and finally a dynamic footer, you can do the following:
"?
Top PHP code here
? "
HTML code
"?
At the end of PHP code here
? "
It gets even better as the PHP code that will only continue from where it left off, so you can do the following:
"?
IF statement (
? "
IF the HTML is correct
"?
Otherwise, ()
? "
If, as the HTML error
"?
)
? "
You must always bear in mind shut down IF statements and loop, though, because it is easy to forget.
Line printer
Be able to put HTML code into your PHP is very useful, but what happens if you want to put a variable value to the code. When using a different echo or print statements, you can not just put the variable names, because this part is not actually part of the PHP code. Instead, you have to just put in a small PHP's.
For example, if you want to print someone's name from the script of the HTML format, you will do the following:
<font face="Arial" size="7" color="red"> the <b> "? echo ($ variablename );?""/ B" "/ Font"
In the above code you have just said that in the following PHP:
"? Echo ($ variablename);?"
This is exactly the same as PHP code is as follows:
"?
Echo ($ variablename);
? "
However, all put on the line.
Conclusion
This tutorial gives you some basic PHP, and should allow you to do a lot of things you would want to. For more in-depth look at you should visit PHP.net, the official home page of PHP. A major omission in this tutorial, you may have noticed is the use of PHP and databases. Because this is one of the main reason is that people use PHP, because there are many options
I will put it in a single PHP / MySQL tutorial. |