Introduction to PHP

Source: Internet
Author: User
Tags date functions mysql php and php and mysql php books sybase zend
Joe Brockmeier briefly introduces the PHP scripting language, discusses the origin, performance, and application Platform of PHP. A simple example of a PHP script illustrates its basic syntax and usage.



If you are working on web-based development, you may have heard of PHP. You may not know exactly what PHP is, how it works, or why it's so hot, but it's time to learn more about PHP. So this article briefly introduces the basic concepts of PHP.

A little background knowledge
PHP as a small open source, as more and more people are aware of its usefulness and gradually developed. Rasmus Lerdorf released the first version of PHP in 1994. Since then, it has developed rapidly and has evolved into version 4.0.3 with countless improvements and refinements in the original release.

PHP is a scripting language that is embedded in HTML and interpreted by the server. It can be used to manage dynamic content, support databases, process session tracking, and even build an entire E-commerce site. It supports many popular databases, including MySQL, PostgreSQL, Oracle, Sybase, Informix, and Microsoft SQL Server.

Dynamic and static content
Why is dynamic content so popular? Let's say you're managing an E-commerce site with 10 products. It is not difficult to hand-write 10 static product pages with the necessary information, forms, and so on as long as the product is not constantly changing or anticipating that it will not change much. But let's say you're adding 10 or more products this month, then more next month, and prices sometimes change or you want to change your site's perception. Then you get bogged down in the dilemma of writing dozens of, perhaps hundreds of static pages, with a manual rewrite.

On the other hand, suppose you start by creating the Product.php page. Instead of static information, it is coded to extract information from the product database and dynamically build a page. You then have a metadata page that provides one, 100, or even 100,000 separate pages based on the information stored in the database. Now webmasters don't simply repeat the task of updating static pages all day long, because updating the information in the corporate database can also update the information on the page. This eliminates the headaches of time lag (the time between changing information in a database and displaying information on a Web site).

Overall, PHP is ideal for work on the Web. But it's not the only way; Perl, Java, JavaScript, ASP, Python, TCL, CGI, and many other methods can generate dynamic content. However, the advantage of PHP is that it is designed for web-based issues and it is open source.

If you are looking for a programming language for word processing or 3D games, PHP may not be the language you need. If you need to run a Web site with dynamic content, database interaction, and E-commerce, read on because PHP is really useful in this regard.

PHP Application Platform
Most regular PHP installations are typically PHP modules that run with Linux or Apache on a variety of UNIX. But if you're using another platform, don't worry. PHP can run on Windows NT and 9x and many other WEB servers. You can find more documentation on PHP in some of the Web sites that introduce the apache/linux/php combination, but it's not the only platform to support PHP.

License and use
How much does it cost to buy an embedded Web scripting language with full functionality? Don't spend a penny? PHP is an open source project, so there is no question of purchasing license fees or restricting usage. You can use PHP to run small, non-profit sites, or run 1 billion-dollar ecommerce sites, and the cost is the same: 0. Not only that, if you want or need to modify PHP, you can modify it.

PHP does not have permission from the GPL, but its own license allows the redistribution of code and/or binary files.

Using PHP
OK, now you're sure you want to really try PHP. Let's take a look at some simple examples so that you have a general idea of PHP. Remember that this is not a way to get a deep understanding of PHP, just a quick start.

"Hello, world!."
To get an idea of PHP, let's take a look at a few very simple PHP scripts. Since "Hello, world!" is a common example, let's write a friendly little "Hello, world!" script.

As mentioned earlier, PHP is embedded in HTML. (Perhaps your file contains almost no HTML, but this file is usually a hybrid of PHP and HTML.) This means that in your normal HTML (or XHTML, where you are at the forefront of the comparison), there will be PHP statements like this:

<body bgcolor= "White" >
<strong>how to say "Hello, world!" </strong>
<?php echo "Hello, world!";? >
<br>
Simple, huh?
</body>





It's simple, isn't it? This is just an "echo" statement, that's all. Of course, that's just not very useful. But it does tell us something about language. (By the way, if you check the HTML output, you'll notice that the PHP code doesn't appear in a file sent from the server to your Web browser.) All PHP that appears on the Web page is processed and stripped from the page, and only pure HTML output is returned to the client from the Web server. )

Print a date and time on a Web page
Now let's do something a little more practical. This example prints the date and time on the Web page.

<body bgcolor= "White" >
<strong>an Example of PHP in action</strong>

<?php echo "The current Date and time is:<br>";
echo Date ("G:i A L, F J Y.");? >
g = The hour, in 12-hour format
i = minutes
A = Print AM or PM, depending ...
L = Print the day of the week
F = Print the Month
j = Print the day of the month
Y = Print the Year-all four digits






This code generates the following output:


The current Date and the are:
11:00 AM Friday, October 20 2000.

Please note that this is rubbed with PHP and HTML. Let's say you already know about HTML, so just explain the PHP code here. The complete PHP reference can be found on php.net (see Resources).

The PHP code starts with a tag <?php and ends with a?>. This tells the server that all content between <?php and?> needs to be parsed using PHP directives, and if they are found, they need to be executed. Note that when you process and service your files, the client receives an ordinary HTML file. People browsing your site cannot see any of the PHP instructions, unless you make a mistake and the server splits the PHP code without first dealing with them.

Regular HTML markup between <?php and?> is typically processed. Please note that this simple script above contains the <br> this branch mark. PHP won't be very useful if you can't make good use of HTML format.

If you want to work with something else, or if you're a forgetful person like me, you might think of annotating your code. The character indicates a comment, the server does not process the content marked by//, and does not pass the content to the client like the annotation in HTML. If there is a standard <!--comment--> between the <?php and?> tags, it is possible for the server to cause errors when parsing it. Obviously, you might not be able to annotate your code as much as I do with this basic feature, but it's a good example.

Finally, note that each PHP function is enclosed in parentheses and ends with a semicolon, which is similar to C or Perl. Because a simple typographical error omits an ending parenthesis or semicolon, it is common to make some grammatical errors, so be sure to check the code. Writing PHP in an editor like Vim or Emacs (which can highlight syntax) helps to eliminate such errors. It allows you to catch errors at once.

The date function is only one of the built-in PHP functions. PHP comes with a number of features that you can use for database connections, creating PDFs, Shockwave, JPG, GIF, PNG, and other image files, sending e-mail, reading and writing files, parsing XML, session processing, talking directly to the browser via HTTP, and many other features.

PHP also allows users to define their own functions. This enables the PHP language to provide a large number of solutions via the Web. Instead of writing everything up from the very beginning. Before you write a function, make sure that you have viewed such things as zend.com, PHP Wizard and, of course, freshmeat, to see if there are any functions you are trying to write (see Resources).

There are already many open source PHP solutions available for headlines, Automatic Updates to news sites, web-based e-mail clients, database management, and more. There is no point in doing this from the beginning. Instead, start with the foundation you've built and customize it to your own solution. If you are just beginning to understand and learn PHP, there is no specific project in mind, then these projects for the use of PHP is still a good example and learning materials.

Conclusion
This brief overview of the PHP scripting language illustrates the PHP features and usage. In future articles, I'll talk about creating a dynamic Web product page by accessing the MySQL database. Before that, check out the following references to PHP for more information.

Resources

Php.net is the main PHP site.
The PHP Wizards contains some documentation and several popular PHP projects.
The author Julie c. Meloni created thickbook.com has many useful tutorials. Julie is the author of Php Essentials (one of the best PHP books).
PHP Builder is another popular web site with tutorials and code libraries on it.
Zend.com contains a lot of useful links and information about PHP. Zend is the optimizer for PHP.
Check out the freshmeat to get some updates on PHP development, as well as some other things.
Please go to perl.com for more information about another popular scripting language.
Please check the MySQL official homepage.
Check out the PHP and MySQL issues that have been resolved on the phpMyAdmin Web site.
The official Python site is always useful.
The Java Lobby has many useful Java information.
About the author
Joe "Zonker" Brockmeier is a Magazine editor for Linux, and he wrote Install for Prima Publishing house, Configure and Customize Slackware Linux. His second book, DocBook Publishing, will be published by Prima earlier in 2001. Can contact him through jbrockmeier@earthlink.net.



The following is the original English

=======================================================

Finally, the perfect language for dynamic content and database interaction

Joe "Zonker" Brockmeier
Senior Editor, User friendly Media
December 2000

Content:


Background
Dynamic vs. Static content
Platforms
Licensing and use
Using PHP
Summary
Resources
About the author



Joe Brockmeier presents a brief introduction to the PHP scripting language with a discussion of PHP ' s origins, Capabilitie s, and the platforms it ' s available on. A Simple PHP Script Example highlights basic syntax and usage.




If you are work with web-based development, your ' ve probably heard about PHP. You are might not know exactly what it are, how it works, or why it's so hot, but your do know it's time to find more about It. So here's a quick intro to the basic concepts that underlie PHP.

A bit of background
PHP started out as a small open source project so evolved as more and more people found out how to useful it was. Rasmus Lerdorf unleashed the "the" of PHP way back in 1994. It has been picking up steam ever since and are now in version 4.0.3 with numerous improvements and refinements over the OR Iginal release.

PHP is a scripting language this is embedded to HTML and interpreted by the server. It can be used to manage dynamic content, work with databases, handle sessions tracking, and even build entire E-commerce s Ites. It works so with a number of popular databases, including MySQL, PostgreSQL, Oracle, Sybase, Informix, and Microsoft SQL Server.

Dynamic vs. Static content
What ' s so hot about dynamic content? Let ' s say ' re managing a E-commerce site with a products. It ' s not so difficult to Hand-code ten static product pages with the requisite information, forms and such, provided Your products don ' t change often and don ' t anticipate much growth. However, let's say you add ten to this month, and then more next month, and occasionally prices change or you w Ant to change the look and feel of your site. Then you ' re stuck re-coding dozens, maybe hundreds, of the static pages by hand.

On the other hand, let's say you to start by creating one page of this is called product.php. Instead of holding static information, it's coded to pull information out of your product database and build a page Dynami Cally. You then have one meta page, can serve up one or one hundred or even a hundred thousand unique pages based on Informat Ion stored in a database. Rather than requiring a web master to spend a entire day doing nothing but monkey-work static Web pages, the INF Ormation can now be updated to the same time the information are changed in the company database. You are eliminate the headache-inducing lag between the time information are changed in the database and the time it makes its Way onto the Web site.

At the risk of generalizing, for work on the Web, PHP is a great way to go. It ' s not the only way to go; Perl, Java, JavaScript, ASP, Python, TCL, CGIs, and probably dozens of the other ways are available for generating dynamic con Tent. However, PHP has the benefit of being designed just for web-based problems and of being a open source project.

If you are looking for a programming language for a word processor or 3D game, then PHP probably isn ' t the way. If you are need to run a Web site with dynamic content, database interaction, and E-commerce, read on, because PHP are going to Be very helpful indeed.

Platforms available for PHP
The most common PHP installation is probably the PHP module running with Apache on Linux or a unix-variant. But If you ' re using something else, don ' t worry. PHP works on Windows NT and 9x, as as and a number of the other WEB servers. You'll find more documentation floating around in the Web that's specific to the apache/linux/php combo and but it isn ' t by a NY means the ' only platform ' PHP is supported on.

Licensing and use
How much might your pay for a full-featured embedded Web scripting language? How about Nothing? PHP is a open source project, so there are no licensing fees or restrictions on. You could run a small, non-profit site, or your could run a billion-dollar e-commerce site with PHP, and the cost is the SA Me:zero. Not so, but the PHP code is available in case you want or need to tweak it.

The PHP is isn't licensed under the GPL, but its own license permits redistribution of code and/or.

Using PHP
Okay, so now you ' re convinced this you want to actually try PHP out. We'll walk through a few simple examples I can get your feet. Keep in mind which is by no means a exhaustive look at PHP, just a quick starter.

"Hello, world!."
To get a feel for PHP, let's look in some very simple PHP scripts. Since "Hello, world!" is a obligatory example, we ll produce a friendly little "Hello, world!" script.

As mentioned earlier, PHP is embedded in HTML. (You could have a file that contains almost no HTML, but usually it ' s a mixture.) That means, amongst your normal HTML (or XHTML if you ' re cutting-edge) you'll have PHP statements like this:

<body bgcolor= "White" >
<strong>how to say "Hello, world!" </strong>
<?php echo "Hello, world!";? >
<br>
Simple, huh?
</body>






Simple, right? Just an "echo" statement, and that ' s it. Of course, which is own isn ' t terribly useful. But It does teach us something about the language. (By the way, if you are examine the HTML output, you'll notice that the PHP code isn't present in the file sent from the Serv Er to your Web browser. All of the "PHP present in" Web page is processed and stripped from the page; The only thing returned to the client of the WEB server is pure HTML output.)

Printing date and time in a Web page
Now we'll do something a little more useful. This example would print out the date and the Web page.

<body bgcolor= "White" >
<strong>an Example of PHP in action</strong>

<?php echo "The current Date and time is:<br>";
echo Date ("G:i A L, F J Y.");? >
g = The hour, in 12-hour format
i = minutes
A = Print AM or PM, depending ...
L = Print the day of the week
F = Print the Month
j = Print the day of the month
Y = Print the Year-all four digits







This code produces the output:


The current Date and the are:
11:00 AM Friday, October 20 2000.

Notice the blend of PHP and HTML here. I ' ll assume that you can already read HTML and so I'll only explain the PHP code. Can find a complete PHP reference online at Php.net (in).

The PHP code begins with the tagged <?php and ends with. This tells the "server" everything between <?php and?> needs to is parsed for PHP instructions, and that if they ' re found, they need to be executed. Note this when your document is processed and served, it would be received by the client as plain HTML. Someone browsing your site would not? Any of your PHP instructions, unless your ' ve made an error and the server spits the M out as-is instead of processing them.

Regular HTML tags within the <?php and?> would be processed normally. Note This simple script above contains a <br> line-break tag. PHP wouldn ' t be very useful if you couldn ' t include HTML formatting as.

If you are going to is working with others, or if you are just plain forgetful like me, you'll want to comment your code as Well. The///characters indicate a comment, which the server won't process or pass on to the client, unlike comments in HTML. If you include a standard <!--comment--> within the <?php and?> tags, it's likely to cause an error Parsed by the server. Obviously, you probably wouldn ' t comment your code quite so much as I have above for such a basic function, but it makes a good example.

Finally, note this is all PHP function is enclosed in parentheses and ends with a semicolon, which'll seem familiar to fan S of C or Perl. It ' s not uncommon to forget a closing parenthesis or semicolon and have a number of parse errors just because of a Typo, so is sure to check them. It's helpful to edit PHP in a editor like Vim or Emacs this is capable of the syntax highlighting. This allows your to catch your errors right away.

The date function is just one of the built-in PHP functions. PHP also comes with functions for database connectivity, creating PDFs, Shockwave, JPG, GIF, PNG, and other graphics files, Sending e-mail, reading and writing files, parsing XML, session handling, talking directly to the browser via HTTP, and M Any other functions.

PHP also allows the user to define their own functions. This makes PHP a language capable of providing a huge the via the Web. Rather than just writing everything from scratch, however, being sure to check sites like zend.com, PHP Wizards, and, of Cour SE, freshmeat to the If what you ' re trying to did has been done already (in).

There are a lot of open sourced PHP solutions for serving banners, automating news sites, web-based e-mail clients, Databa SE management, and much more. There ' s no sense re-inventing the wheel. Instead, start from the foundation which has already been built and customize it into your own. If you are just poking around with PHP to learn it and don ' t have a specific project in mind, these projects are still grea T examples of what you can does with PHP and serve as great learning.

Summary
This brief overview of the PHP scripting language should give your idea of what PHP are capable of and how PHP are being u Sed. In a future article, I'll walk through accessing a MySQL database to create a dynamic WEB product page. Until then, check out the "resources" in PHP below for further information.

Resources

Php.net is the main PHP Web site.
PHP Wizards contains documentation and several popular PHP projects.
Thickbook.com, by Julie C. Meloni, has a number of very helpful tutorials. Julie is the author of Php Essentials, one of the best PHP books available.
The PHP Builder is another popular the PHP site with tutorials and code libraries.
Zend.com contains plenty of useful links and information about PHP. Zend is the PHP Optimizer.
Check out freshmeat to get updates on PHP development and among other things.
Go to perl.com to find out more about another popular scripting language.
Check out the MySQL official home page.
Go to the phpMyAdmin site to the PHP and MySQL handled.
The official Python site is always useful.
The Java Lobby has lots of useful Java info.
About the author
Joe "Zonker" Brockmeier is a contributing editor for Linux Magazine and has written Install, Configure and Customize slack Ware Linux for Prima Publishing. His second book, DocBook Publishing, would be published by Prima in early 2001. He can is reached at jbrockmeier@earthlink.net.


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.