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

Getting Started Guide - PHP Manual Notes

WBOY
Release: 2016-08-08 09:29:34
Original
1406 people have browsed it

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 scripts, but the functions of PHP are far from limited to this. PHP is mainly used in the following three areas:
* Server side script
* Command line script
* Writing desktop applications (PHP-GTK)

Practical scripts

$_SERVER is a special PHP reserved variable, which 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 browsing the page are using.

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

<code>Mozilla/<span>5.0</span> (Windows NT <span>6.1</span>; WOW64; Trident/<span>7.0</span>; rv:<span>11.0</span>) <span>like</span> Gecko
Mozilla/<span>5.0</span> (compatible; MSIE <span>10.0</span>; Windows NT <span>6.2</span>; Trident/<span>6.0</span>)
Mozilla/<span>5.0</span> (compatible; MSIE <span>9.0</span>; Windows NT <span>6.1</span>; Trident/<span>5.0</span>)
Mozilla/<span>4.0</span> (compatible; MSIE <span>8.0</span>; Windows NT <span>6.1</span>; Trident/<span>4.0</span>)
Mozilla/<span>4.0</span> (compatible; MSIE <span>7.0</span>; Windows NT <span>6.0</span>)
Mozilla/<span>4.0</span> (compatible; MSIE <span>6.0</span>; Windows NT <span>5.1</span>)</code>
Copy after login

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

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

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.

<code><span><?php</span><span>$haystack</span> = <span>'hello, world.'</span>;
<span>$needle</span> = <span>'wo'</span>;
<span>echo</span> strpos(<span>$haystack</span>, <span>$needle</span>);</code>
Copy after login

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.

<code><span><<span>form</span><span>action</span>=<span>"action.php"</span><span>method</span>=<span>"post"</span>></span><span><<span>p</span>></span>Name: <span><<span>input</span><span>type</span>=<span>"text"</span><span>name</span>=<span>"name"</span> /></span><span></<span>p</span>></span><span><<span>p</span>></span>Age: <span><<span>input</span><span>type</span>=<span>"text"</span><span>name</span>=<span>"age"</span> /></span><span></<span>p</span>></span><span><<span>p</span>></span><span><<span>input</span><span>type</span>=<span>"submit"</span> /></span><span></<span>p</span>></span><span></<span>form</span>></span></code>
Copy after login

The following code can print data from the form.

<code>Hello, <span><?php</span><span>echo</span> htmlspecialchars(<span>$_POST</span>[<span>'name'</span>]); <span>?></span>. 
You are <span><?php</span><span>echo</span> (int)<span>$_POST</span>[<span>'age'</span>]; <span>?></span> year(s) old.</code>
Copy after login

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 ended)

The above introduces the Getting Started Guide - PHP Manual Notes, including the relevant content. I hope it will be helpful to friends who are interested in PHP tutorials.

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template