What is PHP?

Source: Internet
Author: User
Tags aliases echo command html form learn php md5 hash

What is PHP?

PHP is a back-end dynamic interpretation of computer high-level language, generally used to write or generate dynamic Web pages, mainly responsible for data processing and rendering. (This refers to the use of PHP embedded in the form of the Web page, now you can directly use some JS frame to render the Web page data, PHP is mainly used to do data processing)

Before you learn PHP, you need to know HTML, preferably with C or C++,python and other high-level language foundation
PHP generally needs to be used with Web server software, such as Apache,nginx,iis these server software, of course, these software first need to configure the PHP environment. It is recommended to use phpstudy this software to create your own PHP learning environment on your computer, click Download Phpstudy, if the link expires please go to http://www.phpstudy.net/self-search download

How to use Phpstudy

1. Unzip to the D:\phpstudy\ directory when the download is complete
2. Open Phpstudy This software will automatically open the Apache Web service and MySQL database service, if all the green points indicate that the service opened successfully
Image.png

3. In the browser address bar enter localhost press ENTER, the following page appears to indicate successful installation
Image.png

4. In the D:\phpstudy\www\ directory, put the PHP file you wrote, such as I put a file called test.php, the file content is as follows HelloWorld the title code
5. Enter localhost/test.php in the browser address bar, press ENTER to open the page, output Hello World
Image.png

Hello World

Embed the output HelloWorld directly into the Web page as follows, and output it directly to the Web page using the echo command.

<! DOCTYPE html>
<meta charset= "UTF-8" >
<meta name= "viewport" www.dfgj729.com content= "Width=device-width, initial-scale=1.0" >
<meta http-equiv= "x-ua-compatible" www.thd729.com content= "Ie=edge" >
<title>Document</title>
<body>
<?php
echo "Hello World";
?>
</body>
This file must be saved as a. php file and cannot be saved as an. html file, otherwise the
Comments

As with most programming languages
Used in a single line comment
/* and * * used in multi-line comments

Variable

Initialize and use variables with $ followed by variable names, such as:

<?php
$x = 5;
$y = 6;
$s = "Hello world!";
$z = $x + $y;
echo $z. $s; Output 11Hello World
It can be seen that PHP is a weak type of language, the variable declared with $ can be either a number or a string and other data types, where the string connector is used ". ", you can concatenate multiple characters, such as

<?php
$x = "Hello";
$y = ' world ';
$z = $x. $y;
Echo $z; Output HelloWorld

Attentive friends will notice $y = ' world '; This sentence uses single quotation marks, in fact, PHP inside the single and double quotation marks can represent the string, but must appear in pairs.

PHP statements and variables are strictly case-sensitive, the magic is that PHP has a lot of special global variables, for the programming of the program has a great convenience, will be explained later
Constant

Constants in PHP use the Define () function, which has the following function syntax:

BOOL Define (String $name, mixed $value [, www.douniu828.com bool $case _insensitive = false])
1
which
Name: Constant name (no need to add $ symbol)
Value: Values for constants (can be any data type)
Case_insensitive: Default is case-insensitive, if true, the case of the constant name is not distinguished
Such as:

<?php
Define ("PI", 3.14);
$r = 2;
echo pi* $r * $R; Output 12.56
1
2
3
4
Data type

There are several types of data in PHP:
String (String), Integer (int), float (float), Boolean (Boolean), array (arrays), Object (objects), null (NULL)
The first three types have been used before, a brief introduction of the following string and several later, about the object after the detailed description:

String

Processing functions for commonly used strings
Strlen (): Gets the string length

$s = "Hello world!";
echo strlen ($s); Output 12
1
2
In particular, for Chinese

<?php
echo strlen ("Hello World"); Output 12
1
2
Because the default encoding of the next Chinese is 3 characters, you can use Mb_strlen () to specify the encoded output

<?php
echo mb_strlen ("Hello World", "utf-8"); Output 4
1
2
Strpos (): Finds a character or a specified text within a string.
If a match is found in the string, the function returns the first matching character position. If no match is found, false is returned.

<?php
Echo Strpos ("Hello world!", "World"); Output 6
1
2
MD5 (): Computes the MD5 hash of a string, typically used for simple encryption

<?php
echo MD5 ("password"); Output 5f4dcc3b5aa765d61d8327deb882cf99
1
2
Boolean type

A value of true and false two
For example:

<?php
$x = true;
Var_dump ($x); output bool (TRUE)
1
2
3
The Var_dump () function returns the data type and value of the variable
Array

PHP uses the function array () to create an array with indexed arrays, associative arrays, multidimensional data
Indexed array (the corresponding value in the array is represented by a subscript starting with 0)

<?php
$student = Array ("Kevin", "Bob", "Tom");
echo $student [0]. " Love ". $student [2]; Output Kevin Love Tom
1
2
3
You can use the function count (student) to get the length of the array student to 3
Associative array (an array with key values of "key = = value", each value has its key value)

<?php
$age = Array ("Kevin" =>18, "Bob" =>26, "Tom" =>17);
echo $age [' Kevin ']; Output 18
1
2
3
Multidimensional arrays

<?php
$sites = array
(
"Runoob" =>array
(
"Baidu",
"Http://www.baidu.com"
),
"Google" =>array
(
"Google Search",
"Http://www.google.com"
),
For the sorting of arrays, take a look at this tutorial http://www.runoob.com/php/php-arrays-sort.html

Null value

A NULL value indicates that the variable has no value. Variable data can be emptied by assigning a variable to a null value.

Operator

Many of the operators in PHP are the same as the C language, so there is not much to say, note the following operators:

Operator Action Description
. dot number, collocated operator, for connection string
= = = Absolute equals, used to determine that two variable types and values are equal
+ That means mathematical arithmetic addition, can also be used in two array connection
Not very familiar with operators can look at this tutorial: http://www.runoob.com/php/php-operators.html

Logical statements

Judgment statement

If statement-executes code when conditions are true
If...else statement-Executes a piece of code when the condition is set, and executes another piece of code when the condition is not true
If...elseif....else Statement-executes a block of code when one of several conditions is established
Switch statement-executes a block of code when one of several conditions is established
In particular, remember to use a break in the switch statement and jump out of each option.

Looping statements

Loops like the C language
While-loops through code blocks as long as the specified condition is true
Do...while-Executes the code block first, and then repeats the loop when the specified condition is set
For-loop execution code block specified number of times
Focus on the PHP-specific foreach Loop:
foreach is primarily used to iterate through an array, as follows:

<?php
$age = Array ("Kevin" =>18, "Bob" =>26, "Tom" =>17);
foreach ($age as $name = $a)
{
echo $name. " The age of ". $a." <br> ";
}
/* Output
Kevin's age is 18.
Bob's age is 26.
Tom's age is 17.
*/
1
2
3
4
5
6
7
8
9
10
11
Function

PHP declares a function like this

<?php
Function name (argument list)
{
The code to execute
}
1
2
3
4
5
The parameter can have a default value, the return value of the function is returned directly with return, can return the return value of any data type
Variable scope of the function:
As in the following program

<?php
$x = 5; Global variables
function MyTest ()
{
$y = 10; Local variables
echo "Variable x is: $x"; Will error notice:undefined variable:x in D:\phpstudy\WWW\test.php on line 6
echo "<br>"; Line break
echo "Variable y is: $y"; Output variable y is: 5
}
MyTest ();
1
2
3
4
5
6
7
8
9
10
Why does the 6th line of code make an error? Because inside the function is not directly accessible to the global variable x, the function internal initialization of the variable y can only work within the function, if you need to use global variables within the function, you need to declare within the function, using the global directive

<?php
$x = 5; Global variables
function MyTest ()
{
Global $x; Declaring Global variables $x
$y = 10; Local variables
echo "Variable x is: $x"; Output variable x is: 5
echo "<br>"; Line break
echo "Variable y is: $y"; Output variable y is: 5
}
MyTest ();
1
2
3
4
5
6
7
8
9
10
11
Special Variables for PHP

Magic variable

__FILE__: The full path and file name of the file.
__LINE__: The current line number in the text.
__DIR__: The current directory where the files are located.
__FUNCTION__: Used inside a function, returns the name of the function.
__CLASS__: Used in a class to return the name of a class.
__NAMESPACE__: Returns the name of the current namespace.

Super Global variables

This content is detailed in the tutorial: http://www.runoob.com/php/php-superglobals.html
GLOBALS: is an associative array, any declared global variables will be saved to the array, the key value of the array is the name of the global variable, the value is the value of the global variable _server: is a containing such as header information (headers), PATH, and an array of information, such as the script location (scripts locations). The items in this array are created by the WEB server. There is no guarantee that each server will provide all the items, the server may ignore some, or provide some items that are not listed here.
REQUEST: Used to collect data submitted by an HTML form. _post:, collect the data that the form uploads via POST (Specify the form's method= "post" in HTML)
$_get: It is also widely used to collect form data and also for URL-sent data

Name space

Namespaces are designed to avoid the equivalent of aliases introduced by classes/variables/functions or third-party class library name collisions within PHP, and these conflicts can be avoided by using different namespaces and setting aliases.

declaring namespaces:
namespace name;
Use a namespace:
Use name as alias;
where "as aliases" can also be omitted.

Usually put in the first line of the PHP code file.
You can also use multiple namespaces within a file, separating different namespaces with {}
Such as:

<?php
Namespace MyProject {

Const CONNECT_OK = 1;
Class Connection {/* ... */}
function connect () {/* ... */}
}

namespace {//Global code
Session_Start ();
$a = Myproject\connect ();
Echo Myproject\connection::start ();
}
1
2
3
4
5
6
7
8
9
10
11
12
13
Object oriented

If you have learned C + + or Python,java, you know what object-oriented programming is.
declaring classes

<?php
Class name
Each class has a this variable, which represents the object itself and can be used to invoke a variable or method of the class through the This object.
Create the object using the New keyword, as follows:

<?php
Class Student
{
function __construct ($name, $age) {//constructor
$this->name = $name;
$this->age = $age;
}
}
Instantiating an Object
$xiaoming = new Student ("Xiaoming", 18);
Echo $xiaoming->age; Output 18

Access control
Public: The members of a public class can be accessed from anywhere.
Protected (Protected): A protected class member can be accessed by itself and its subclasses and parent classes.
Private: A private class member can only be accessed by the class in which it is defined.
Abstract class
Any class, if at least one of its methods is declared abstract, then the class must be declared abstract.
A class that is defined as abstract cannot be instantiated. Use the keyword abstract to declare the class, as follows

<?php
Abstract class ClassName
{
Content of abstract classes

Static keyword
Declaring a class property or method as static (static) can be accessed directly without instantiating the class.
Static properties cannot be accessed through an object that a class has instantiated (but static methods can).
Because static methods do not need to be called through an object, the pseudo-variable $this is not available in a static method.
A static property cannot be accessed by an object through the operator.

There are a lot of things to be aware of about PHP's object-oriented programming, but perhaps the basic syntax that you need to master in your Web-based backend programming is these, and you can read the tutorial "Beginner's Tutorial" a few times in PHP: http://www.runoob.com/php/php-tutorial.html

OK to here basically you have understood the syntax base of PHP, and then you need to learn PHP some of the standard library application, such as file operation, GD graphics library (make image Verification code), with PHP for MySQL database operation.

For the convenience of Web server backend development, you can try to understand PHP development framework, such as domestic ThinkPHP5, the current popular laravel, and many people use the Yii

What is PHP?

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.