search
HomeBackend DevelopmentPHP TutorialSummary of PHP Basics (1)_PHP Tutorial

Summary of PHP Basics (1)_PHP Tutorial

Jul 13, 2016 pm 05:47 PM
echolangphpscriptxmloneuseBaseSummarizerecommendsupportmarkstyle

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

3)
echo "short style tag"; ?>
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
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
How can you check if a PHP session has already started?How can you check if a PHP session has already started?Apr 30, 2025 am 12:20 AM

In PHP, you can use session_status() or session_id() to check whether the session has started. 1) Use the session_status() function. If PHP_SESSION_ACTIVE is returned, the session has been started. 2) Use the session_id() function, if a non-empty string is returned, the session has been started. Both methods can effectively check the session state, and choosing which method to use depends on the PHP version and personal preferences.

Describe a scenario where using sessions is essential in a web application.Describe a scenario where using sessions is essential in a web application.Apr 30, 2025 am 12:16 AM

Sessionsarevitalinwebapplications,especiallyfore-commerceplatforms.Theymaintainuserdataacrossrequests,crucialforshoppingcarts,authentication,andpersonalization.InFlask,sessionscanbeimplementedusingsimplecodetomanageuserloginsanddatapersistence.

How can you manage concurrent session access in PHP?How can you manage concurrent session access in PHP?Apr 30, 2025 am 12:11 AM

Managing concurrent session access in PHP can be done by the following methods: 1. Use the database to store session data, 2. Use Redis or Memcached, 3. Implement a session locking strategy. These methods help ensure data consistency and improve concurrency performance.

What are the limitations of using PHP sessions?What are the limitations of using PHP sessions?Apr 30, 2025 am 12:04 AM

PHPsessionshaveseverallimitations:1)Storageconstraintscanleadtoperformanceissues;2)Securityvulnerabilitieslikesessionfixationattacksexist;3)Scalabilityischallengingduetoserver-specificstorage;4)Sessionexpirationmanagementcanbeproblematic;5)Datapersis

Explain how load balancing affects session management and how to address it.Explain how load balancing affects session management and how to address it.Apr 29, 2025 am 12:42 AM

Load balancing affects session management, but can be resolved with session replication, session stickiness, and centralized session storage. 1. Session Replication Copy session data between servers. 2. Session stickiness directs user requests to the same server. 3. Centralized session storage uses independent servers such as Redis to store session data to ensure data sharing.

Explain the concept of session locking.Explain the concept of session locking.Apr 29, 2025 am 12:39 AM

Sessionlockingisatechniqueusedtoensureauser'ssessionremainsexclusivetooneuseratatime.Itiscrucialforpreventingdatacorruptionandsecuritybreachesinmulti-userapplications.Sessionlockingisimplementedusingserver-sidelockingmechanisms,suchasReentrantLockinJ

Are there any alternatives to PHP sessions?Are there any alternatives to PHP sessions?Apr 29, 2025 am 12:36 AM

Alternatives to PHP sessions include Cookies, Token-based Authentication, Database-based Sessions, and Redis/Memcached. 1.Cookies manage sessions by storing data on the client, which is simple but low in security. 2.Token-based Authentication uses tokens to verify users, which is highly secure but requires additional logic. 3.Database-basedSessions stores data in the database, which has good scalability but may affect performance. 4. Redis/Memcached uses distributed cache to improve performance and scalability, but requires additional matching

Define the term 'session hijacking' in the context of PHP.Define the term 'session hijacking' in the context of PHP.Apr 29, 2025 am 12:33 AM

Sessionhijacking refers to an attacker impersonating a user by obtaining the user's sessionID. Prevention methods include: 1) encrypting communication using HTTPS; 2) verifying the source of the sessionID; 3) using a secure sessionID generation algorithm; 4) regularly updating the sessionID.

See all articles

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 Tools

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment