Home Backend Development PHP Tutorial Summary of PHP Basics (1)_PHP Tutorial

Summary of PHP Basics (1)_PHP Tutorial

Jul 13, 2016 pm 05:47 PM
echo lang php script xml one use Base Summarize recommend support mark style

PHP supports a total of 4 markup styles:
1)
echo "This is an xml style tag"; //Recommended to use
?>
2)

3)

4)
<%
echo "This is ASP style markup";
%>
/*
If you want to use short style and asp style, you need to configure it in php.ini, and then set short_open_tag and asp_tags to ON
*/


PHP supports three types of comments:
// c++ style single line comments
/* */ c-style multi-line comments
# Shell style comments
/*
Do not appear the ?> sign in a single-line comment, because the interpreter will think that the php script has ended and execute the content after it
*/


Data types in php:
PHP supports a total of 8 primitive types, including 4 scalar types (boolean, integer, float, string)
Two composite types: array, object
Two special types: resource,null


About boolean
The Boolean type has two values: true/false
*In php, not only false is false, 0 0.0 "0" "" arrays that only declare no values ​​​​are false


About string
There are three ways to define strings: single quotes (recommended), double quotes, and delimiter
The difference between single quotes and double quotes:
1) When using single quotes, you only need to escape the single quotes. When using double quotes, you need to escape characters such as "$
2) If you use single quotes, the content inside will be output as is. If you use double quotes, PHP will spend some time processing string escaping and variable parsing
Delimiter:
$s = << String content
str; //No spaces in front, no different from double quotes


About integer
Integer numbers can be expressed in decimal, octal, or hexadecimal. If it is octal, add leading 0. If it is hexadecimal, add 0x
If illegal numbers (8,9) appear in octal, the following numbers will be ignored
If the given value exceeds the maximum range that the int type can represent, it will be treated as a float type. This situation is called integer overflow


About float
The value of floating point numbers is only an approximation. Try to avoid comparisons of floating point numbers because the results are often inaccurate


About null
Not case sensitive,
There are three situations when a null value is assigned: no value has been assigned, a value is assigned to null, and a variable has been processed by the unset() function?
The function to determine whether it is null is is_null(), and the return value is boolean type
Starting from php4, unset() no longer has a return value


Casting:
(boolean) Convert to boolean
(string) ;
(integer) ;
(float) ;
(array) ;
(object) ;
*When converted to boolean, null, 0 and unassigned variables or arrays will be converted to false, and others will be true
When converting to an integer, false is 0 for Boolean type and 1 is true for true. The decimal point of the floating point number will be rounded off. If the character type starts with a number, non-numeric digits will be intercepted. Otherwise, 0

will be output.


Type conversion can also be completed through the settype() function,
bool settype(mixed var,string type)
The parameter var is the specified variable, the parameter type is the specified type, and there are 7 optional values ​​(boolean, float, integer, array, null, object, string)
The settype function will convert the type of the original variable, while forced type conversion only generates a temporary variable, and the type of the original variable will not change


Functions for monitoring data types:
is_bool() is_string() is_float()/is_double() is_integer()/is_int() is_null() is_array() is_object() is_numeric()
is_numeric() checks whether the variable is a number or a string composed of numbers


php constants:
Use the define() function to define constants:
define(string constant_name,mixed value,case_insensitive)
constant_name constant name
value constant value
case_sensitive is optional, specifies whether it is case sensitive, true means insensitive
There are two ways to get the value of a constant. One is to use the constant name to get the value directly, and the other is to use the constant() function
mixed constant(string const_name) //Return the value of the constant
To determine whether a constant has been defined, you can use the defined() function
bool defined(string constant_name)


PHP predefined constants:
__FILE__ php program file path
__LINE__ The line where this constant is located
PHP_VERSION program version
PHP_OS The name of the operating system that executes the php parser


php variable:
There is reference assignment in PHP. Use different names to access the contents of the same variable. When the value of one variable is changed, the other one will also change. Use the & symbol to represent
$i = "Hello";
$s = &$i;
Variable scope
Local variables are variables defined inside a function, and their scope is the function
Global variables are variables defined outside of all functions. Their scope is the entire php file, but cannot be accessed within user-defined functions. If you want to use global variables within user-defined functions, you must use the global statement
Static variables can retain variable values ​​after the function call is completed
Such as:
$i = 'hello';
function fun(){
global $i;
echo $i; //output hello
}

?>


Mutable variables:
A variable variable is a unique variable that allows the name of a variable to be changed dynamically. The working principle is that the name of the variable is determined by the value of another variable. The implementation process is to add an extra dollar sign in front of the variable
$i = 'abc';
$abc = 'hello';
echo $$i; //hello

www.2cto.com
PHP predefined variables:
$_SERVER['SERVER_ADDR'] The IP address of the server where the script is currently running
$_SERVER['SERVER_NAME'] The name of the server host where the script is currently running
$_SERVER['REQUEST_METHOD'] Request method when accessing the page
$_SERVER['REMOTE_ADDR'] The user ip that is browsing the current page
$_SERVER['REMOTE_HOST'] The host name of the user who is browsing the current page
$_SERVER['REMOTE_PORT'] The port used by users to connect to the server


Excerpted from Youth China Airlines’ column

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/478472.htmlTechArticlephp supports a total of 4 tag styles: 1) ?php echo This is an xml style tag; //Recommended? 2) script language = php echo This is the script style tag; /script 3) ? echo short style...
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1672
14
PHP Tutorial
1277
29
C# Tutorial
1257
24
PHP and Python: Different Paradigms Explained PHP and Python: Different Paradigms Explained Apr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

Choosing Between PHP and Python: A Guide Choosing Between PHP and Python: A Guide Apr 18, 2025 am 12:24 AM

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP and Python: A Deep Dive into Their History PHP and Python: A Deep Dive into Their History Apr 18, 2025 am 12:25 AM

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

PHP's Impact: Web Development and Beyond PHP's Impact: Web Development and Beyond Apr 18, 2025 am 12:10 AM

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

PHP vs. Python: Use Cases and Applications PHP vs. Python: Use Cases and Applications Apr 17, 2025 am 12:23 AM

PHP is suitable for web development and content management systems, and Python is suitable for data science, machine learning and automation scripts. 1.PHP performs well in building fast and scalable websites and applications and is commonly used in CMS such as WordPress. 2. Python has performed outstandingly in the fields of data science and machine learning, with rich libraries such as NumPy and TensorFlow.

The Continued Use of PHP: Reasons for Its Endurance The Continued Use of PHP: Reasons for Its Endurance Apr 19, 2025 am 12:23 AM

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

PHP: An Introduction to the Server-Side Scripting Language PHP: An Introduction to the Server-Side Scripting Language Apr 16, 2025 am 12:18 AM

PHP is a server-side scripting language used for dynamic web development and server-side applications. 1.PHP is an interpreted language that does not require compilation and is suitable for rapid development. 2. PHP code is embedded in HTML, making it easy to develop web pages. 3. PHP processes server-side logic, generates HTML output, and supports user interaction and data processing. 4. PHP can interact with the database, process form submission, and execute server-side tasks.

C   and XML: Exploring the Relationship and Support C and XML: Exploring the Relationship and Support Apr 21, 2025 am 12:02 AM

C interacts with XML through third-party libraries (such as TinyXML, Pugixml, Xerces-C). 1) Use the library to parse XML files and convert them into C-processable data structures. 2) When generating XML, convert the C data structure to XML format. 3) In practical applications, XML is often used for configuration files and data exchange to improve development efficiency.

See all articles