


Benniao learns PHP (5) Declaration and use of functions_PHP tutorial
Beniao learns php (5) declaration and use of functions
1. Function declaration
<?php /** * function 函数名(参数1, 参数2.... ){ * 函数体; * 返回值; * } */ $sum = sum(3, 4); echo $sum; function sum($x, $y){ $sum = 0; $sum = $x * $x + $y + $y; return $sum; } ?>
2. Scope of variables
Local variables: Variables declared in a function are local variables and can only be used within your own function .
Global variables: Variables declared outside a function are global variables. They can be used after the variable is declared until the end of the entire script, including in functions and { }.
2.1 Knowledge point: PHP variables cannot be distinguished whether they are declared or used
<?php $a = 10; function demo($a){ $a += 10; echo $a . '<br>'; } demo($a); echo $a; // 结果: 20, 10 // PHP的变量分不出 $a 是声明还是使用 ?>2.2 Knowledge point: When using global variables in a function, you must use the global keyword. The variable after declaring global is the global variable
<?php $a = 10; function demo() { global $a; $a += 10; echo $a . " demo <br>"; } function test() { global $a; $a += 5; echo $a . "test <br>"; } echo $a . " ----<br>"; // 10 ---- demo(); // 20 demo echo $a . " !!!!<br>"; // 20 !!!! demo(); // 30 demo echo $a . " @@@@<br>"; // 30 @@@@ test(); // 35 test echo $a . " ####<br>"; // 35 #### ?>
Static variables: can only be declared in functions (classes) and cannot be declared globally. Use the static keyword to modify before the variable.
2.3 Knowledge point: Static variables are stored in static code blocks. Its value can be shared between multiple calls of a function, but it is only declared to memory when the function is called for the first time
When called in the future, it will not be declared but used directly (similar to Java).
<?php function test(){ static $a = 0; $a++; echo $a."<br>"; } test(); // 1 test(); // 2 test(); // 3 ?>
3. Variable function
In fact, this is very similar to JS, for example: define $var = hello, the next time you use $var(), you will look for the function hello() with the same name as the variable value
<?php function one($a, $b){ return $a + $b; } function two($a, $b){ return $a * $a + $b * $b; } function three($a, $b){ return $a * $a * $a + $b * $b * $b; } //$var = one; //$var = "two"; $var = three; echo "结果:".$var(3, 4)."<br>"; // 91 ?>
4. System function
4.1 Regular functionsbool copy (string source, string dest)
4.2 With mixed, mixed means that any type of data can be transmitted
bool chown (string filename, mixed user)
4.3 Function with & parameter, indicating reference assignment. This parameter cannot pass a value, only a variable can be passed, and then the function changes the value of the variable,
When we use this variable, the value also changes (the same as passing references in Java)
bool arsort (array &array [, int sort_flags])<?php $arr=array(1, 9, 5, 8, 3, 4); sort($arr); print_r($arr); ?>4.4 Default function, a function with [], indicates that this parameter is optional. If you pass a value, the value you passed will be used. If no value is passed, the default value will be used
bool arsort (array &array [, int sort_flags])
<?php function demo($a=1, $b=20, $c){ echo "### $a ### $b ### $c ###<br>"; } demo(8,9); ?>4.5 Parameter function with..., ... means that any number of parameters can be passed
int array_unshift (array &array, mixed var [, mixed ...])
<?php function demo(){ $sum = 0; for($i = 0; $i < func_num_args(); $i++){ $sum += func_get_arg($i); } return $sum; } echo demo(1, 2, 3, 4, 5, 6, 7, 8, 9); ?>
4.6 Callback function with callback, that is, when calling this function, we need to pass in a function (function name, function name string)
array array_filter (array input [, callback callback])
<?php function demo($n){ if($n % 2== 0){ return true; }else{ return false; } } $a = array(1, 2, -3, 4, -5, 6, -7, 8, 9); print_r(array_filter($a, demo)); // Array ( [1] => 2 [3] => 4 [5] => 6 [7] => 8 ) ?>

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.

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

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.

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

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.

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

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

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.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

Dreamweaver CS6
Visual web development tools

Dreamweaver Mac version
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

WebStorm Mac version
Useful JavaScript development tools
