Home >Backend Development >PHP Tutorial >Getting Started with PHP - PHP Manual Notes

Getting Started with PHP - PHP Manual Notes

PHP中文网
PHP中文网Original
2016-08-08 09:29:371195browse

I once briefly learned PHP by reading "PHP and MySQL Web Development" and with the help of a universal search engine. This time I am going to study the system. The reference material is PHP Manual.

What PHP can do

PHP is mainly used for server-side script programs, but the functions of PHP are far from limited to this. PHP is mainly used in the following three areas:

  • Server scripts

  • Command line scripts

  • Writing desktop applications (PHP-GTK)

Practical scripts

$_SERVER is a special PHP reserved variable that contains all the information provided by the web server and is called a superglobal variable. You can use $_SERVER['HTTP_USER_AGENT'] to check what browser the visitors to the page are using.

For IE browser, the value of $_SERVER['HTTP_USER_AGENT'] may be:

Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like Gecko
Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; Trident/6.0)
Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)
Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; Trident/4.0)
Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0)
Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)

You can determine whether the user agent is IE browser by calling the strpos() function.

<?php 
$ua = $_SERVER['HTTP_USER_AGENT'];
if(strpos($ua, 'Trident') !== FALSE || strpos($ua, 'MSIE') !== FALSE) {
	echo 'You are using Internet Explorer.';
} else {
	echo 'You are not using Internet Explorer.';
}

This code involves the use of strpos(), !== and FALSE.

strpos() is a built-in function of PHP. Its function is to search for another string (needle) in a string (haystack). If found, the function returns the position of the needle relative to the beginning in the haystack; if not, it returns FALSE.

<?php 
$haystack = 'hello, world.';
$needle = 'wo';
echo strpos($haystack, $needle);

For the above code, the result returned by strpos() is 7. For the specific value returned by strpos(), the calculation method may be different for spaces and Chinese, which will be discussed later.

Processing forms

PHP’s way of processing forms is very convenient. You can use the super global variable $_POST to obtain data. Use the following method to define a simple HTML form. When the user fills out the form and clicks the submit button, the page action.php will be called.

Name:Age:

The following code can print data from the form.

Hello, . 
You are  year(s) old.

This code also involves the use of htmlspecialchars() and (int). htmlspecialchars() enables special characters in HTML to be correctly encoded, so that users will not inject HTML tags or Javascript codes into the page.

Tools

If a worker wants to do his job well, he must first sharpen his tools.

With a good tool, you can get twice the result with half the effort. For efficiency, I like to use VIM and run code from the command line.

As for tools, this post talks about it well. Is there an alternative tool for cmd under window? - windows - SegmentFault.

WampServer and XAMPP are recommended for PHP environment.

(Full text ends)

The above is the content of PHP Getting Started Guide - PHP Manual Notes. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn