从Perl到PHP(英文版)Perl to PHP Migration Introduction Perl and PHP are great scripting languages. Perl has led the web scripting language race for quite a while without much competition. But now PHP has been born. Don't get me wrong- Perl is an excellent programming language and is still very, very useful. But PHP was created solely for use on the web and therefore has many benefits over Perl. Perl is still used for application programming and other purposes, and in order to accommodate for this, it has many downfalls that prevent it from being the perfect language for web development. PHP isn't perfect either, but in my opinion, it's close. This article is not a comparison between the two languages, but rather a reference for Perl programmers looking to learn PHP. I have gotten several E-Mails and comments saying "but you can do all that in Perl!" or "Perl/Embperl are good too!" For this article I'm assuming that the audience is Perl programmers interested in PHP programming. Since the two languages are so similar, it makes sense to have an article explaining how to use one's knowledge of Perl towards PHP. Yes, I realize that all of the things here are possible in Perl, sometimes even easier - my point is to show you how to do what you already know how to do in Perl, in PHP. PHP is very similar to Perl. The most important difference is that PHP is embedded into HTML, so your page doesn't need to be entirely code (HTML isn't code, as far as I'm concerned). There's an embedded version of Perl - Embperl - but I view that as just an attempt to retain the same language but use it towards a purpose that it was not designed for a purpose that was not originally intended. Since PHP was designed solely for the internet, it has all the qualities necessary for an embedded programming language, and doesn't make any sacrifices to make it useful for other purposes. PHP is a language being developed solely for the Internet, so it doesn't include the features not needed for web scripting and adds extra functionality for the purpose. I suspect that PHP will advance more quickly for web development in the future than Perl and Embperl, since the PHP developers don't have to worry about other purposes beyond the web. This article will explain some of the differences between the two languages and give Perl programmers hints on how to convert their Perl programs to PHP or how to begin programming in PHP. It is assumed that all readers are fluent in Perl. Embedded Scripting What I mean by "embedded" is that a page can be HTML with added bits of PHP code. In Perl you'd have to put the HTML in a print statement. In PHP, you simply surround the code by <? and ?> or the <?php and ?> "tags." For example:
Welcome to my webpage. Below you can see fascinating PHP code that prints the numbers from 1 to 10.<P> <?php for ($i = 1; $i <= 10; $i++) {
Everything except what is enclosed in <? and ?&rt; tags is regular HTML; the PHP code is inside the <? ?>. This is very useful. It means that you don't need to use print << to output long pieces of HTML. Another place where this is useful is for using short snippets of code, such as:
Note that only part of the <font> tag uses PHP code. Cookies and Parameters PHP's implementation of cookies is one example of how PHP makes your life easier. Although setting cookies is nearly the same as setting cookies in Perl, using already-stored cookies is far easier. PHP presets a variable for each cookie the page has access to (ie. each cookie set for the domain in use) with the value of the cookie. For example, if a cookie 'username' is set to the value 'chatgirl74' then each page that can access the cookie automatically has a variable, $username, with the value 'chatgirl74'. If you've used cookies in Perl, you'll know that this is a huge convenience. The implementation of parameters is similar. Any parameter sent to a page - no matter which method is used - becomes a variable in PHP. If you used the page index.php?username=chatgirl74 then there's a variable set on the page with the name $username and the value 'chatgirl74'. It works the same way with the POST method. If you have a field <input type=text name=username> on one page, the receiving page will have the $username variable set, regardless of whether you use the POST method or the GET method. 上一篇:将VB转化成PHP(英文) 下一篇:如何让Ultraedit支持PHP 更多相关文章
|
推荐文章
精彩文章
|