search
HomeBackend DevelopmentPHP TutorialPHP variables and constants study notes_PHP tutorial

PHP variables and constants study notes_PHP tutorial

Jul 21, 2016 pm 03:53 PM
phpPass valuevariableaddressstudyconstantsupplyofnotesAssignment



Assignment by address of variables

In PHP 3, variables are always assigned by value. PHP 4 provides another way to assign values ​​to variables: assignment by address. To use pass-by-address assignment, simply append an ampersand (&) to the variable to be assigned (the source variable). This means that the new variable simply references the original variable, and changes to the new variable will affect the original variable, and vice versa.


$foo = 'Bob';
$bar = &$foo;
$bar = "My name is $bar";
echo $bar;
echo $foo; Given the variable bar, when the value of variable bar changes, the value of variable foo also changes.

About (super) global variables

The declaration of PHP global variables is declared when the variable is referenced, rather than when defining or assigning the variable in the first line of the program to define whether it is a global or local variable.


$a = 1;
$b = 2;

function Sum()
{
global $a, $b;
$b = $a + $b;
}

Sum();
echo $b;
?>




If global variables are not declared using global in the function Sum(), the program will report an undefined variable error.

Of course, there are some variables in PHP that do not require global declaration within the functional scope of a certain program. These variables are called superglobal variables, and these superglobal variables are basically not user-defined, but Some variables predefined by PHP, such as $_GET, $_POST, $_COOKIE, etc.

About variable variables

The more interesting variable variables in PHP, such as $a="bruce", can also be expressed as $bruce using $$a, that is, the variable variable is used two dollar signs.

But in $$a[1] , is $a[1] used as a variable, or $$a is used as a variable and the value of index [1] in the variable is taken out? There is no sequential relationship here, but ${$a[1]} or ${$a}[1] is used to represent the above two situations.

============================================== =============

About constants

Constants are different from variables. From the moment a constant is defined, its scope is the global

Quantity default They are case-sensitive, and by convention constant identifiers are always uppercase

There is no dollar sign ($) in front of the constant

Once a constant is defined, it cannot be redefined or undefined

Constants can only be defined using the define() function, not through assignment statements

For example, define("MYNAME","cnbruce") defines a MYNAME constant with the value "cnbruce"


define("MYNAME","cnbruce");
$MYNAME="cnrose";
echo MYNAME;
echo $MYNAME;
?> ;



In addition, how to output the values ​​of constants and variables together requires PHP string operations. Use English periods (.) to merge string connections into new ones. String, similar to & in ASP.

echo MYNAME.",".$MYNAME; The output is "cnbruce,cnrose"


Like predefined variables in variables, PHP also has predefined constants (or magic constant), that is, no define() function definition is required. For example,

__FILE__ represents the full path and file name of the file, similar to the current file in Server.Mappath in ASP


echo __FILE__;
?> ;



PHP predefined constants are divided into:
Kernel predefined constants, constants defined in the PHP kernel, Zend and SAPI modules
Standard predefined constants, in PHP Default defined constants




http://www.bkjia.com/PHPjc/318766.html

www.bkjia.com

truehttp: //www.bkjia.com/PHPjc/318766.htmlTechArticleAbout variable assignment by address In PHP3, variables are always assigned by value. PHP4 provides another way to assign values ​​to variables: assignment by address. Use pass-by-address assignment, i.e. simply append a...
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
What is the difference between unset() and session_destroy()?What is the difference between unset() and session_destroy()?May 04, 2025 am 12:19 AM

Thedifferencebetweenunset()andsession_destroy()isthatunset()clearsspecificsessionvariableswhilekeepingthesessionactive,whereassession_destroy()terminatestheentiresession.1)Useunset()toremovespecificsessionvariableswithoutaffectingthesession'soveralls

What is sticky sessions (session affinity) in the context of load balancing?What is sticky sessions (session affinity) in the context of load balancing?May 04, 2025 am 12:16 AM

Stickysessionsensureuserrequestsareroutedtothesameserverforsessiondataconsistency.1)SessionIdentificationassignsuserstoserversusingcookiesorURLmodifications.2)ConsistentRoutingdirectssubsequentrequeststothesameserver.3)LoadBalancingdistributesnewuser

What are the different session save handlers available in PHP?What are the different session save handlers available in PHP?May 04, 2025 am 12:14 AM

PHPoffersvarioussessionsavehandlers:1)Files:Default,simplebutmaybottleneckonhigh-trafficsites.2)Memcached:High-performance,idealforspeed-criticalapplications.3)Redis:SimilartoMemcached,withaddedpersistence.4)Databases:Offerscontrol,usefulforintegrati

What is a session in PHP, and why are they used?What is a session in PHP, and why are they used?May 04, 2025 am 12:12 AM

Session in PHP is a mechanism for saving user data on the server side to maintain state between multiple requests. Specifically, 1) the session is started by the session_start() function, and data is stored and read through the $_SESSION super global array; 2) the session data is stored in the server's temporary files by default, but can be optimized through database or memory storage; 3) the session can be used to realize user login status tracking and shopping cart management functions; 4) Pay attention to the secure transmission and performance optimization of the session to ensure the security and efficiency of the application.

Explain the lifecycle of a PHP session.Explain the lifecycle of a PHP session.May 04, 2025 am 12:04 AM

PHPsessionsstartwithsession_start(),whichgeneratesauniqueIDandcreatesaserverfile;theypersistacrossrequestsandcanbemanuallyendedwithsession_destroy().1)Sessionsbeginwhensession_start()iscalled,creatingauniqueIDandserverfile.2)Theycontinueasdataisloade

What is the difference between absolute and idle session timeouts?What is the difference between absolute and idle session timeouts?May 03, 2025 am 12:21 AM

Absolute session timeout starts at the time of session creation, while an idle session timeout starts at the time of user's no operation. Absolute session timeout is suitable for scenarios where strict control of the session life cycle is required, such as financial applications; idle session timeout is suitable for applications that want users to keep their session active for a long time, such as social media.

What steps would you take if sessions aren't working on your server?What steps would you take if sessions aren't working on your server?May 03, 2025 am 12:19 AM

The server session failure can be solved through the following steps: 1. Check the server configuration to ensure that the session is set correctly. 2. Verify client cookies, confirm that the browser supports it and send it correctly. 3. Check session storage services, such as Redis, to ensure that they are running normally. 4. Review the application code to ensure the correct session logic. Through these steps, conversation problems can be effectively diagnosed and repaired and user experience can be improved.

What is the significance of the session_start() function?What is the significance of the session_start() function?May 03, 2025 am 12:18 AM

session_start()iscrucialinPHPformanagingusersessions.1)Itinitiatesanewsessionifnoneexists,2)resumesanexistingsession,and3)setsasessioncookieforcontinuityacrossrequests,enablingapplicationslikeuserauthenticationandpersonalizedcontent.

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

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

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.

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool