The best language for dynamic content and database interaction: PHP

Source: Internet
Author: User
Tags date contains html tags php and php code php script what php mysql database
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 Time is: 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 it. This tells the server that <?php ?> all content between and is syntactically parsed with PHP directives, and that they need to be executed if they are found. 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.

Typically <?php ?> , the normal HTML tags are processed between and. Note that the simple script above contains <br> this branch tag. 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 <?php ?> there is a standard between and tags <!-- comment --> , 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.



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.