PHP Basics Primer (i) "The best programming language in the World"

Source: Internet
Author: User
Tags getting started with php php language php basics scalar

Brief introduction

---------

PHP (Hyper-text preprocessor) is a common open source scripting language. The grammar absorbs the C language, Java and Perl features, is conducive to learning, widely used, mainly for the field of web development. PHP's unique syntax mixes the syntax of C, Java, Perl, and PHP's own creation. It can execute Dynamic Web pages more quickly than CGI or Perl. Dynamic pages made in PHP are compared to other programming languages, and PHP is executed in HTML (an application under the standard Universal Markup Language), which is much more efficient than CGI, which generates HTML markup entirely; PHP can also execute post-compilation code, Compilation can achieve encryption and optimize code execution, making code run faster.

   No more nonsense, ↓↓↓↓↓↓↓.

PHP Web Base

I. Static website & Dynamic website

1, static website: pure HTML page, the content of the page is fixed, no interaction with the background, no database support;
2, Dynamic Web page: The content of the page will be based on background interaction to show different content, need to interact with the background server, the need for database support.

Second, support the language of the dynamic website
Java---JSP
PHP----PHP
ASP----ASP

Third, the common server
Apache:apache's servers are used to parse PHP-like websites. Port number: 80
Another server under the Tomcat:apache company, used to parse the JSP class Web site. Port number: 8080

The default port number for the HTTP protocol is 80, so you do not need to enter the port when using Apache server.
Local ip:127.0.0.1 or localhost can be used directly

Iv. Common Database
Mysql,access,oracle,sqlserver ...

MySQL is an open source free database software that has less data storage than Oracle, but performs more efficiently than Oracle.
PHP systems are often used in conjunction with MySQL, but PHP also supports links to almost any major browser.

V. B/s C/s
b/S: Browser/server structure. A Web site that users only need to access via a browser. Most of the operation functions are centralized in the background server.
C/S: Client/server structure. The user needs to install the program that the client uses. The client can share certain operation functions.

Vi. Common development of PHP integration environment
Install a software, built in a variety of development needs of the software or language environment
wamp:windows+apache+mysql+php
lamp:linux+apache+mysql+php
Phpstudy:
APPServer:
>>>php developed the preferred Linux system, but PHP is compatible with a variety of operating systems.
(Windows,linux,unix,macos ...)

Basics of getting Started with PHP

**php "+" only operation function, no connection function. Connection function with "." Connection
* * Keywords: Types of PHP tags, instruction separators in PHP, variables in PHP, false in PHP, declaration of strings in PHP, pseudo types in PHP, data type conversions in PHP, type of judgment in PHP, output in PHP, and constants in PHP

I. Types of PHP tags

php file The default language is still HTML code, if you want to use PHP in PHP file syntax, you have to put PHP code in the PHP language tag, PHP supports four kinds of language tags:
①<?php?> This is the official PHP writing, but also our main use of the wording!
>>> Note: If the page is pure PHP code, it is recommended to omit the end of the notation "?>";

②<script language= "PHP" ></script> long marker style. This notation is always supported, but we do not recommend it.
③<??> short markup style, default does not open, you need to modify the php.ini file "Short_open_tag=on" to use. However, this syntax conflicts with XML files and is not recommended for use.
④<%%> ASP style, default does not open, you need to modify the php.ini file "Asp_tags=on" in order to use. However, this type of writing conflicts with ASP files and JSP files, and is not recommended for use.

Second, the instruction delimiter in PHP
1, the "Structure definition statement" in PHP (branch, loop, class, function, etc. {} end), do not need to use a semicolon end.
2, the "function execution statement" in PHP, without the end of {}, must use the end of a semicolon.
3, Special case: End the last statement before the label?>, you can omit the semicolon.

third, the variables in PHP
1, the variables in PHP, declaration and use, must start with $.
2, PHP is a weak type of language! Variables need not be declared, directly assign values to variables, and can modify the data type of the variable at any time during subsequent assignment;
3, PHP can be used to declare multiple variables at the same time, but not separated by commas; eg: $a = $b = $c =10;
4. Common variable functions:
unset (); Delete and release variables;
isset (); Check whether the variable is set;
Empty (); Verify that the variable is null (not set or null is empty);
5, the name of the variable: can only be composed of numbers, letters, underscores, the beginning can not be a number. Also, variables in PHP are case-sensitive! $name $Name $NAME are different variables! However, PHP built-in functions or class names are case-insensitive! echo Echo is all valid!
6. There are 8 types of data in PHP:
• 4 types of scalar :
Boolean type: Boolean
integral type: integer
floating point type: float/double
strings: string
• 2 types of composite :
Arrays: Array
objects: Object
• 2 special types :
Resources: Resource
null: null
7, the scope of the integer:-(2^31-1) ~ ~ ~ (2^31-1), over this range, will automatically be converted to floating-point type.


Iv. true and False conversions in PHP

1, Boolean type: False True for true
2, Integer type: 0 is false non 0 is true
3, floating-point type: 0.0 is false non-0. Not 0 is true
4, String type:" "," 0 "False rest all true
5, array type: Empty array false other true
6, Object type: All true. (only PHP4 version, empty object is False)
7, NULL type: All false
8, Resource: Type all true

V. Declaration of strings in PHP
PHP supports three ways of declaring strings:
1, ":
2, "":
3. <<< (delimiter):
$str = <<<s (bound identifier)

The bound terminator must be fixed, and no other content, including spaces, should be in a row;
The bounding identifier can be any character, as long as the start is consistent with the Terminator;
S (End of delimiter)

4, three different ways of declaring:
① delimiter function, the default is the same as double quotes. (But you can put double quotes in the delimiter)
② single quotes, cannot parse variables, and double quotes, delimiters can parse variables.
Note: When you parse a variable with double quotes, be aware that the variable name needs to be "hahaha{$num}hahaha "Hahaha${num}hahaha" is available!
③ single quotation marks can be placed in double quotes, double quotation marks can be placed in single quotation marks. But you cannot put quotation marks of the same type.

VI, pseudo-type in PHP
pseudo-type: Not a real data type. It exists just to tell the programmer which type of data is available, and which types are often used when composing help documents.
mixed: The delegate parameter can be of many different data types.
number: The parameter can be either integer or float.
callback: Represents a parameter can be a callback function.

Vii. data type conversions in PHP
1, automatic type conversion: When the operation is expressed, boolean,null,string and other types, will be automatically converted to an integer or float type
Null-->0
True-->1
False-->0
String to a number before a numeric character,
If not, switch to 0 "123a"-->123 "A123"-->0

2, coercion type conversion: According to our needs, forcing the variable to other types of variables.

The types that can be cast are as follows:

$ New variable = (new type) $ original variable; $str = (String) 1;
//new type of name, you can use full spelling, or you can use abbreviations. Integer/int Boolean/bool is OK.
① Use () to declare a new type of strong turn:
(integer) (int)---converted to integral type
(Boolean) (BOOL)---converted to Boolean
( float), (double), (real)---converted to floating-point type
(String)---converted to a string
(Array)---convert an array
(object)---converted to an object
② using Settype (); function strong turn:
bool SetType (mixed var,string type); The first parameter represents a variable of any type, the second parameter represents the data type of the string type, and the return value is a Boolean type that indicates whether the conversion succeeded.
the difference between the two types of conversions:
①: It is to assign the result after conversion to a new variable;
Section ②: Directly modifies the data type of the original variable.


Viii. Common functions for judging data types in PHP
Is_bool (): Determines whether it is a Boolean type
is_int (), Is_integer (), and Is_long (): Determines whether it is an integral type.
is_float (), is_double (), and Is_real (): Determines whether it is a floating-point type
is_string (): Determines whether it is a string
Is_array (): Determines whether the array
Is_object (): Determines whether the object
Is_resource (): Determines whether the resource type
Is_null (): Determines whether it is null
is_scalar (): Determines whether it is a scalar
is_numeric (): Determines whether it is any type of numeric and numeric string
is_callable (): Determine if the function name is valid

Ix. output Statements in PHP
echo "..."; the value of the output variable
Var_dump (num); Data type & value of output variable [& some additional information]
Print_r (arr); specifically for printing arrays

Single-line Comment


/*
* Multi-line Comment
*/


/**
* Documentation Comments
*/


# Script Comments
#echo "① form";

10. Constants in PHP
1, the definition of the constant: bool Define (String constant name, mixed constant value [, BOOL is case-sensitive]);
2, the constant note:
① constants are defined, only the Define () function can be used;
② constant names, which in principle require the use of uppercase declarations. and must not carry the $ symbol ($ can only be a variable);
after the ③ constant is declared, the default global scope is valid. There's no scope to say.
④ constants can not be changed, and can not be deleted! Unset,settype and other functions are invalid
⑤ constants are case-sensitive by default!!! However, when you declare a constant, you can change the Define third argument to true to be case insensitive.
⑥ constants must be defined with define () before they can be used. If you use an undeclared constant, the default is to a constant string. However, there will be warnings.
var_dump (num);-->string "num"
the value of the ⑦ constant can only be a scalar boolean String Float Integer
3, you can use Echo constant ("NUM"); Read the constant value! Attention!!!
You can use Get_defined_constants () to get all the defined constants!! Includes system-customized n-Multiple constants.

PHP Basics Primer (i) "The best programming language in the World"

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.