ΠΏΡΠΎΠ²Π΅ΡΠΊΠ° Π½Π°Π»ΠΈΡΠΈΡ ΠΏΠ΅ΡΠ΅ΠΌΠ΅Π½Π½ΠΎΠΉ php
isset
(PHP 4, PHP 5, PHP 7, PHP 8)
isset β ΠΠΏΡΠ΅Π΄Π΅Π»ΡΠ΅Ρ, Π±ΡΠ»Π° Π»ΠΈ ΡΡΡΠ°Π½ΠΎΠ²Π»Π΅Π½Π° ΠΏΠ΅ΡΠ΅ΠΌΠ΅Π½Π½Π°Ρ Π·Π½Π°ΡΠ΅Π½ΠΈΠ΅ΠΌ, ΠΎΡΠ»ΠΈΡΠ½ΡΠΌ ΠΎΡ null
ΠΠΏΠΈΡΠ°Π½ΠΈΠ΅
ΠΠΏΡΠ΅Π΄Π΅Π»ΡΠ΅Ρ, Π±ΡΠ»Π° Π»ΠΈ ΡΡΡΠ°Π½ΠΎΠ²Π»Π΅Π½Π° ΠΏΠ΅ΡΠ΅ΠΌΠ΅Π½Π½Π°Ρ Π·Π½Π°ΡΠ΅Π½ΠΈΠ΅ΠΌ ΠΎΡΠ»ΠΈΡΠ½ΡΠΌ ΠΎΡ null
ΠΡΠ»ΠΈ Π±ΡΠ»ΠΈ ΠΏΠ΅ΡΠ΅Π΄Π°Π½Ρ Π½Π΅ΡΠΊΠΎΠ»ΡΠΊΠΎ ΠΏΠ°ΡΠ°ΠΌΠ΅ΡΡΠΎΠ², ΡΠΎ isset() Π²Π΅ΡΠ½ΡΡ true ΡΠΎΠ»ΡΠΊΠΎ Π² ΡΠΎΠΌ ΡΠ»ΡΡΠ°Π΅, Π΅ΡΠ»ΠΈ Π²ΡΠ΅ ΠΏΠ°ΡΠ°ΠΌΠ΅ΡΡΡ ΠΎΠΏΡΠ΅Π΄Π΅Π»Π΅Π½Ρ. ΠΡΠΎΠ²Π΅ΡΠΊΠ° ΠΏΡΠΎΠΈΡΡ ΠΎΠ΄ΠΈΡ ΡΠ»Π΅Π²Π° Π½Π°ΠΏΡΠ°Π²ΠΎ ΠΈ Π·Π°ΠΊΠ°Π½ΡΠΈΠ²Π°Π΅ΡΡΡ, ΠΊΠ°ΠΊ ΡΠΎΠ»ΡΠΊΠΎ Π±ΡΠ΄Π΅Ρ Π²ΡΡΡΠ΅ΡΠ΅Π½Π° Π½Π΅ΠΎΠΏΡΠ΅Π΄Π΅Π»ΡΠ½Π½Π°Ρ ΠΏΠ΅ΡΠ΅ΠΌΠ΅Π½Π½Π°Ρ.
Π‘ΠΏΠΈΡΠΎΠΊ ΠΏΠ°ΡΠ°ΠΌΠ΅ΡΡΠΎΠ²
ΠΠΎΠ·Π²ΡΠ°ΡΠ°Π΅ΠΌΡΠ΅ Π·Π½Π°ΡΠ΅Π½ΠΈΡ
ΠΡΠΈΠΌΠ΅ΡΡ
ΠΡΠΈΠΌΠ΅Ρ #1 ΠΡΠΈΠΌΠ΅Ρ ΠΈΡΠΏΠΎΠ»ΡΠ·ΠΎΠ²Π°Π½ΠΈΡ isset()
// Π ΡΠ»Π΅Π΄ΡΡΡΠ΅ΠΌ ΠΏΡΠΈΠΌΠ΅ΡΠ΅ ΠΌΡ ΠΈΡΠΏΠΎΠ»ΡΠ·ΡΠ΅ΠΌ var_dump Π΄Π»Ρ Π²ΡΠ²ΠΎΠ΄Π°
// Π·Π½Π°ΡΠ΅Π½ΠΈΡ, Π²ΠΎΠ·Π²ΡΠ°ΡΠ°Π΅ΠΌΠΎΠ³ΠΎ isset().
$a = «test» ;
$b = «anothertest» ;
Π€ΡΠ½ΠΊΡΠΈΡ ΡΠ°ΠΊΠΆΠ΅ ΡΠ°Π±ΠΎΡΠ°Π΅Ρ Ρ ΡΠ»Π΅ΠΌΠ΅Π½ΡΠ°ΠΌΠΈ ΠΌΠ°ΡΡΠΈΠ²ΠΎΠ²:
ΠΡΠΈΠΌΠ΅Ρ #2 isset() ΠΈ ΡΡΡΠΎΠΊΠΎΠ²ΡΠ΅ ΠΈΠ½Π΄Π΅ΠΊΡΡ
Π Π΅Π·ΡΠ»ΡΡΠ°Ρ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΈΡ Π΄Π°Π½Π½ΠΎΠ³ΠΎ ΠΏΡΠΈΠΌΠ΅ΡΠ°:
ΠΡΠΈΠΌΠ΅ΡΠ°Π½ΠΈΡ
ΠΠ°ΠΌΠ΅ΡΠ°Π½ΠΈΠ΅: ΠΠΎΡΠΊΠΎΠ»ΡΠΊΡ ΡΡΠΎ ΡΠ·ΡΠΊΠΎΠ²Π°Ρ ΠΊΠΎΠ½ΡΡΡΡΠΊΡΠΈΡ, Π° Π½Π΅ ΡΡΠ½ΠΊΡΠΈΡ, ΠΎΠ½Π° Π½Π΅ ΠΌΠΎΠΆΠ΅Ρ Π²ΡΠ·ΡΠ²Π°ΡΡΡΡ ΠΏΡΠΈ ΠΏΠΎΠΌΠΎΡΠΈ ΠΏΠ΅ΡΠ΅ΠΌΠ΅Π½Π½ΡΡ ΡΡΠ½ΠΊΡΠΈΠΉ.
ΠΡΠΈ ΠΈΡΠΏΠΎΠ»ΡΠ·ΠΎΠ²Π°Π½ΠΈΠΈ isset() Π½Π° Π½Π΅Π΄ΠΎΡΡΡΠΏΠ½ΡΡ ΡΠ²ΠΎΠΉΡΡΠ²Π°Ρ ΠΎΠ±ΡΠ΅ΠΊΡΠ°, Π±ΡΠ΄Π΅Ρ Π²ΡΠ·ΡΠ²Π°ΡΡΡΡ ΠΏΠ΅ΡΠ΅Π³ΡΡΠΆΠ΅Π½Π½ΡΠΉ ΠΌΠ΅ΡΠΎΠ΄ __isset(), Π΅ΡΠ»ΠΈ ΠΎΠ½ ΡΡΡΠ΅ΡΡΠ²ΡΠ΅Ρ.
Π‘ΠΌΠΎΡΡΠΈΡΠ΅ ΡΠ°ΠΊΠΆΠ΅
User Contributed Notes 30 notes
I, too, was dismayed to find that isset($foo) returns false if ($foo == null). Here’s an (awkward) way around it.
Of course, that is very non-intuitive, long, hard-to-understand, and kludgy. Better to design your code so you don’t depend on the difference between an unset variable and a variable with the value null. But «better» only because PHP has made this weird development choice.
In my thinking this was a mistake in the development of PHP. The name («isset») should describe the function and not have the desciption be «is set AND is not null». If it was done properly a programmer could very easily do (isset($var) || is_null($var)) if they wanted to check for this!
The new (as of PHP7) ‘null coalesce operator’ allows shorthand isset. You can use it like so:
You can safely use isset to check properties and subproperties of objects directly. So instead of writing
isset($abc) && isset($abc->def) && isset($abc->def->ghi)
or in a shorter form
you can just write
without raising any errors, warnings or notices.
How to test for a variable actually existing, including being set to null. This will prevent errors when passing to functions.
«empty() is the opposite of (boolean) var, except that no warning is generated when the variable is not set.»
!empty() mimics the chk() function posted before.
in PHP5, if you have
I tried the example posted previously by Slawek:
$foo = ‘a little string’;
echo isset($foo)?’yes ‘:’no ‘, isset($foo[‘aaaa’])?’yes ‘:’no ‘;
He got yes yes, but he didn’t say what version of PHP he was using.
I tried this on PHP 5.0.5 and got: yes no
But on PHP 4.3.5 I got: yes yes
Any foreach or similar will be different before and after the call.
To organize some of the frequently used functions..
Return Values :
Returns TRUE if var exists and has value other than NULL, FALSE otherwise.
isset expects the variable sign first, so you can’t add parentheses or anything.
With this simple function you can check if an array has some keys:
If you regard isset() as indicating whether the given variable has a value or not, and recall that NULL is intended to indicate that a value is _absent_ (as said, somewhat awkwardly, on its manual page), then its behaviour is not at all inconsistent or confusing.
Here is an example with multiple parameters supplied
= array();
$var [ ‘val1’ ] = ‘test’ ;
$var [ ‘val2’ ] = ‘on’ ;
The following code does the same calling «isset» 2 times:
= array();
$var [ ‘val1’ ] = ‘test’ ;
$var [ ‘val2’ ] = ‘on’ ;
Note that isset() is not recursive as of the 5.4.8 I have available here to test with: if you use it on a multidimensional array or an object it will not check isset() on each dimension as it goes.
Imagine you have a class with a normal __isset and a __get that fatals for non-existant properties. isset($object->nosuch) will behave normally but isset($object->nosuch->foo) will crash. Rather harsh IMO but still possible.
// pretend that the methods have implementations that actually try to do work
// in this example I only care about the worst case conditions
// if property does not exist <
echo «Property does not exist!» ;
exit;
// >
>
$obj = new FatalOnGet ();
Uncomment the echos in the methods and you’ll see exactly what happened:
On a similar note, if __get always returns but instead issues warnings or notices then those will surface.
The following is an example of how to test if a variable is set, whether or not it is NULL. It makes use of the fact that an unset variable will throw an E_NOTICE error, but one initialized as NULL will not.
The problem is, the set_error_handler and restore_error_handler calls can not be inside the function, which means you need 2 extra lines of code every time you are testing. And if you have any E_NOTICE errors caused by other code between the set_error_handler and restore_error_handler they will not be dealt with properly. One solution:
?>
Outputs:
True False
Notice: Undefined variable: j in filename.php on line 26
This will make the handler only handle var_exists, but it adds a lot of overhead. Everytime an E_NOTICE error happens, the file it originated from will be loaded into an array.
ΠΡΠΎΠ²Π΅ΡΠΊΠ° ΡΡΡΠ΅ΡΡΠ²ΠΎΠ²Π°Π½ΠΈΡ ΠΏΠ΅ΡΠ΅ΠΌΠ΅Π½Π½ΠΎΠΉ
ΠΠΎΠΌΠΎΡΡ Π² Π½Π°ΠΏΠΈΡΠ°Π½ΠΈΠΈ ΠΊΠΎΠ½ΡΡΠΎΠ»ΡΠ½ΡΡ , ΠΊΡΡΡΠΎΠ²ΡΡ ΠΈ Π΄ΠΈΠΏΠ»ΠΎΠΌΠ½ΡΡ ΡΠ°Π±ΠΎΡ Π·Π΄Π΅ΡΡ.
ΠΡΠΎΠ²Π΅ΡΠΊΠ° ΡΡΡΠ΅ΡΡΠ²ΠΎΠ²Π°Π½ΠΈΡ Π΄ΠΈΡΡΠ΅ΠΊΡΠΎΡΠΈΠΈ
ΠΠ΄ΡΠ°Π²ΡΡΠ²ΡΠΉΡΠ΅.ΠΡΠΆΠ½ΠΎ ΡΠΎΠ·Π΄Π°ΡΡ ΠΏΠ°ΠΏΠΊΡ.ΠΡΠ»ΠΈ ΠΏΠΈΡΡ @mkdir(PATH.’/images/catalog/’.$inUser->id.’/’);.
ΠΡΠΎΠ²Π΅ΡΠΊΠ° ΡΡΡΠ΅ΡΡΠ²ΠΎΠ²Π°Π½ΠΈΡ ΡΠ°ΠΉΠ»Π°
ΠΡΠΆΠ½ΠΎ ΠΏΡΠΎΠ²Π΅ΡΠΈΡΡ ΡΡΡΠ΅ΡΡΠ²ΠΎΠ²Π°Π½ΠΈΠ΅ ΡΠ°ΠΉΠ»Π°. Π‘Π΅ΠΉΡΠ°Ρ ΠΏΡΠΎΠ²Π΅ΡΡΡ Π΅ΡΡΡ Π»ΠΈ ΠΈΠ·ΠΎΠ±ΡΠ°ΠΆΠ΅Π½ΠΈΠ΅, Π° Π½ΡΠΆΠ½ΠΎ Π»ΡΠ±ΠΎΠΉ ΡΠ°ΠΉΠ», Π½ΠΎ.
ΠΠΎΠ±Π°Π²Π»Π΅Π½ΠΎ ΡΠ΅ΡΠ΅Π· 29 ΡΠ΅ΠΊΡΠ½Π΄
Π΄Π° ΡΡΠΎ Π½Π΅ ΡΠ°Π±ΠΎΡΠ°Π΅Ρ ΡΠΎΠ»ΡΠΊΠΎ ΡΡΠΎ ΠΏΡΠΎΠ²Π΅ΡΠΈΠ»
ΠΠΎΠ±Π°Π²Π»Π΅Π½ΠΎ ΡΠ΅ΡΠ΅Π· 1 ΠΌΠΈΠ½ΡΡΡ
ΠΊΠ°ΡΠΎΡΠ΅ Π·Π°Π΄Π°ΡΠ° ΡΠ°ΠΊΠ°Ρ Ρ ΡΠΎΠ·Π΄Π°Ρ ΡΠΎΡΠΌΡ ΠΈΠ· Π½Π΅ΡΠΊΠΎΠ»ΡΠΊΠΎ Π²ΠΎΠΏΡΠΎΡΠΎΠ² Π΅ΡΠ»ΠΈ Π²ΡΠ΅ Π΄Π°Π½Π½ΡΠ΅ Π²Π΅ΡΠ½Ρ Π²ΡΠ²Π΅ΡΡΠΈ Π½Π° ΡΡΡ 2 Π΅ΡΠ»ΠΈ Ρ
ΠΎΡΡ ΠΎΠ΄Π½Π° Π½Π΅ΠΏΡΠ°Π²ΠΈΠ»ΡΠ½ΠΎ ΠΎΡΠΏΡΠ°Π²ΠΈΡΡ Π½Π° ΡΡΡ 3
ΠΠΎΠ±Π°Π²Π»Π΅Π½ΠΎ ΡΠ΅ΡΠ΅Π· 1 ΠΌΠΈΠ½ΡΡΡ
Π»Π°Π΄Π½ΠΎ ΡΠ΅Π±ΡΡΠ° ΡΠΏΡ Π·Π° ΡΡΡΠ΄Ρ Ρ Π΄Π°Π»ΡΡΠ΅ ΡΠ°ΠΌ ΠΏΠΎΠ΄ΡΠΌΠ°Ρ ΡΡΠΎ Π΅ΡΡ ΠΌΠΎΠΆΠ½ΠΎ ΡΠ΄Π΅Π»Π°ΡΡ
ΠΠΎΠΌΠΎΡΡ Π² Π½Π°ΠΏΠΈΡΠ°Π½ΠΈΠΈ ΠΊΠΎΠ½ΡΡΠΎΠ»ΡΠ½ΡΡ , ΠΊΡΡΡΠΎΠ²ΡΡ ΠΈ Π΄ΠΈΠΏΠ»ΠΎΠΌΠ½ΡΡ ΡΠ°Π±ΠΎΡ Π·Π΄Π΅ΡΡ.
ΠΡΠΎΠ²Π΅ΡΠΊΠ° ΡΡΡΠ΅ΡΡΠ²ΠΎΠ²Π°Π½ΠΈΡ ΡΠ΅ΡΡΠΈΠΈ
ΠΡΡΡ Π½Π΅ΠΊΠ°Ρ Π·Π°Π΄Π°ΡΠ°. ΠΡΡΡ Π½Π° ΡΠ΅ΡΠ²Π΅ΡΠ΅ ΠΏΠ°ΠΏΠΊΠ° Ρ Π²ΡΠ΅ΠΌΠ΅Π½Π½ΡΠΌΠΈ ΡΠ°ΠΉΠ»Π°ΠΌΠΈ temp_dir, Π² Π½Π΅ΠΉ ΠΏΠΎΠΌΠ΅ΡΠ°ΡΡΡΡ ΡΠ°ΠΉΠ»Ρ.
If else Π½Π΅ΡΠΊΠΎΠ»ΡΠΊΠΎ ΡΡΠ»ΠΎΠ²ΠΈΠΉ Ρ ΠΏΡΠΎΠ²Π΅ΡΠΊΠΎΠΉ ΡΡΡΠ΅ΡΡΠ²ΠΎΠ²Π°Π½ΠΈΡ ΠΏΠ΅ΡΠ΅ΠΌΠ΅Π½Π½ΠΎΠΉ
ΠΠΎΠ±ΡΡΠΉ Π΄Π΅Π½Ρ. ΡΠ²Π°ΠΆΠ°Π΅ΠΌΡΠ΅ ΡΠΎΡΡΠΌΡΠ°Π½Π΅! ΠΠΎΠ·ΠΌΠΎΠΆΠ½ΠΎ, Π²ΠΎΠΏΡΠΎΡ ΠΏΠΎΠΊΠ°ΠΆΠ΅ΡΡΡ ΠΌΠ½ΠΎΠ³ΠΈΠΌ Π³Π»ΡΠΏΡΠΌ ΠΈ ΡΠ»Π΅ΠΌΠ΅Π½ΡΠ°ΡΠ½ΡΠΌ, Π½ΠΎ Π½Π΅.
ΠΡΠΎΠ²Π΅ΡΠΊΠ° ΡΡΡΠ΅ΡΡΠ²ΠΎΠ²Π°Π½ΠΈΡ ΠΊΠ°ΡΡΠΈΠ½ΠΊΠΈ Π½Π° ΡΠ΅ΡΠ²Π΅ΡΠ΅
Π²ΡΠ΅ΠΌ ΠΏΡΠΈΠ²Π΅Ρ! Π² ΠΎΡΠ²Π΅Ρ Π½Π° ΠΌΠΎΠΉ Π·Π°ΠΏΡΠΎΡ ΡΠ΅ΡΠ΅Π· Π½Π΅ΠΊΠΎΠ΅ API ΠΏΡΠΈΡ
ΠΎΠ΄ΠΈΡ ΠΎΡΠ²Π΅Ρ Ρ Π½ΡΠΆΠ½ΡΠΌΠΈ ΠΌΠ½Π΅ ΠΏΠ°ΡΠ°ΠΌΠ΅ΡΡΠ°ΠΌΠΈ.
ΠΠ΅ΡΠΊΠΎΠ»ΡΠΊΠΎ ΠΏΡΠΎΠ²Π΅ΡΠΎΠΊ php ΠΏΡΠΈΠΌΠ΅Ρ
ΠΡΠ΅Π΄ΡΡΠ°Π²ΠΈΠΌ ΡΠΈΡΡΠ°ΡΠΈΡ, ΠΊΠΎΠ³Π΄Π° Ρ Π²Π°Ρ Π΅ΡΡΡ ΠΊΠ°ΠΊΠΎΠΉ-ΡΠΎ ΠΊΠΎΠ΄ Ρ ΠΏΡΠΎΠ²Π΅ΡΠΊΠ°ΠΌΠΈ, Π½Ρ Π½Π°ΠΏΡΠΈΠΌΠ΅Ρ ΠΏΡΠΎΠ²Π΅ΡΠΈΡΡ Π½Π° ΠΏΡΡΡΠΎΡΡ, ΠΏΡΠΎΠ²Π΅ΡΠΈΡΡ Π½Π° ΠΊΠ°ΠΊΠΎΠ΅-ΡΠΎ ΡΠ»ΠΎΠ²ΠΎ ΠΈ Ρ.Π΄.
Π Π²ΠΎΡ Π²Π°ΠΌ Π½ΡΠΆΠ½ΠΎ Π΅ΡΡ Π΄ΠΎΠ±Π°Π²ΠΈΡΡ Π½Π΅ΡΠΊΠΎΠ»ΡΠΊΠΎ ΠΏΡΠΎΠ²Π΅ΡΠΎΠΊ ΠΈ Π΅ΡΠ»ΠΈ Π²Ρ ΡΡΠΎ Π΄Π΅Π»Π°Π»ΠΈ, ΡΠΎ ΠΏΠΎΠ½ΠΈΠΌΠ°Π΅ΡΠ΅, ΡΡΠΎ ΠΏΡΠΈΠ΄Π΅ΡΡΡ ΡΠ΄Π΅Π»Π°ΡΡ. Π Π·Π°Π²ΠΈΡΠΈΠΌΠΎΡΡΠΈ ΠΎΡ ΠΊΠΎΠ»ΠΈΡΠ΅ΡΡΠ²Π° Β«ifΒ» Π½ΡΠΆΠ½ΠΎ ΡΠ΄Π΅Π»Π°ΡΡ ΠΈ ΡΡΠΎΠ»ΡΠΊΠΎ ΠΆΠ΅ Β«elseΒ», Π΅ΡΠ»ΠΈ ΠΎΠ΄Π½ΠΎ ΡΠ°Π·Π²Π΅ΡΠ²Π»Π΅Π½ΠΈΠ΅ β ΡΡΠΎ Π΅ΡΠ΅ Π»Π°Π΄Π½ΠΎ, Π° Π΅ΡΠ»ΠΈ 3 ΠΈΠ»ΠΈ 5 ΠΈΠ»ΠΈ Π²ΠΎΠΎΠ±ΡΠ΅ 10, ΡΡΠΎ-ΡΠΎ ΠΊΠ°ΠΊ-ΡΠΎ ΡΡΠ°Π·Ρ Π½Π΅ ΠΏΠΎ ΡΠ΅Π±Π΅ ΡΡΠ°Π½ΠΎΠ²ΠΈΡΡΡ ΠΎΡ ΡΡΠΎΠ³ΠΎ ΠΊΠΎΠ»ΠΈΡΠ΅ΡΡΠ²ΠΎ ΠΊΠΎΠ΄Π°, ΠΊΠΎΡΠΎΡΠΎΠ΅ Π½ΡΠΆΠ½ΠΎ Π½Π°ΠΏΠΈΡΠ°ΡΡ!
Π‘ΠΎΠ³Π»Π°ΡΠΈΡΠ΅ΡΡ, ΡΡΠΎ ΡΡΠΎ Π΄ΠΎΠ²ΠΎΠ»ΡΠ½ΠΎ Π½Π΅ΡΠ΄ΠΎΠ±Π½ΠΎ! ΠΠ° ΠΈ Π² Π±ΠΎΠ»ΡΡΠΎΠΌ ΠΊΠΎΠ΄Π΅, ΠΊΠΎΡΠΎΡΡΠΉ Π²Ρ Π½Π°ΠΏΠΈΡΠ°Π»ΠΈ, ΡΠ΅ΡΠ΅Π· Π½Π΅Π΄Π΅Π»Ρ Π½Π΅Π²ΠΎΠ·ΠΌΠΎΠΆΠ½ΠΎ ΡΠ°Π·ΠΎΠ±ΡΠ°ΡΡΡΡ!
Π Ρ ΠΎΡΠ΅ΡΡΡ Π² ΠΎΠ΄Π½ΠΎΠΌ ΠΌΠ΅ΡΡΠ΅ Π½Π°ΠΏΠΈΡΠ°ΡΡ ΡΠ»ΠΎΠ²ΠΎ ΠΈ ΡΡΠΎΠ±Ρ Π½Π° Π½Π΅Π³ΠΎ Π±ΡΠ»Π° ΠΏΡΠΎΠ²Π΅ΡΠΊΠ°.
ΠΡΠ΅ ΡΠΏΠΎΡΠΎΠ±Ρ ΡΠ΄Π΅Π»Π°ΡΡ Π½Π΅ΡΠΊΠΎΠ»ΡΠΊΠΎ ΠΏΡΠΎΠ²Π΅ΡΠΎΠΊ
ΠΠ΅ΡΠΊΠΎΠ»ΡΠΊΠΎ ΠΏΡΠΎΠ²Π΅ΡΠΎΠΊ ΡΡΡΠ΅ΡΡΠ²ΠΎΠ²Π°Π½ΠΈΡ Π½Π΅ΡΠΊΠΎΠ»ΡΠΊΠΈΡ ΠΏΠ΅ΡΠ΅ΠΌΠ΅Π½Π½ΡΡ
ΠΠ΄Π½ΠΎΠ²ΡΠ΅ΠΌΠ΅Π½Π½ΠΎ ΠΏΡΠΎΠ²Π΅ΡΠΈΡΡ ΡΡΡΠ΅ΡΡΠ²ΠΎΠ²Π°Π½ΠΈΠ΅ Π½Π΅ΡΠΊΠΎΠ»ΡΠΊΠΈΡ ΠΏΠ΅ΡΠ΅ΠΌΠ΅Π½Π½ΡΡ !
ΠΡΠ΅Π΄ΠΏΠΎΠ»ΠΎΠΆΠΈΠΌ, ΡΡΠΎ Ρ Π½Π°Ρ Π΅ΡΡΡ Π½Π΅ΡΠΊΠΎΠ»ΡΠΊΠΎ ΠΏΠ΅ΡΠ΅ΠΌΠ΅Π½Π½ΡΡ :
ΠΠ°ΠΌ Π½ΡΠΆΠ½ΠΎ ΠΏΡΠΎΠ²Π΅ΡΠΈΡΡ ΡΡΡΠ΅ΡΡΠ²ΡΡΡ Π»ΠΈ Π΄Π°Π½Π½ΡΠ΅ ΠΏΠ΅ΡΠ΅ΠΌΠ΅Π½Π½ΡΠ΅ ΠΎΠ΄Π½ΠΎΠ²ΡΠ΅ΠΌΠ΅Π½Π½ΠΎ Π²ΡΠ΅ Π²ΠΌΠ΅ΡΡΠ΅, Π² ΡΡΠ»ΠΎΠ²ΠΈΠΈ if Π·Π°ΠΏΠΈΡΡΠ²Π°Π΅ΠΌ Π²ΡΠ΅ ΠΏΠ΅ΡΠ΅ΠΌΠ΅Π½Π½ΡΠ΅ ΡΠ΅ΡΠ΅Π· and :
Π Π΄Π΅Π»Π΅Π΅ Π½Π°ΠΌ Π½ΡΠΆΠ΅Π½ ΠΏΡΠΈΠΌΠ΅Ρ. ΠΏΡΠ΅Π΄ΠΏΠΎΠ»ΠΎΠΆΠΈΠΌ ΡΡΠΎ ΠΎΠ΄Π½Π° ΠΈΠ· Π²ΡΡΠ΅ ΠΏΠ΅ΡΠ΅ΡΠΈΡΠ»Π΅Π½Π½ΡΡ ΠΏΠ΅ΡΠ΅ΠΌΠ΅Π½Π½ΡΡ Π½Π΅ ΡΡΡΠ΅ΡΡΠ²ΡΠ΅Ρ($peremennaya_4 = «»;), Π° Π²ΡΡΠ΅ ΠΏΡΠΈΠ²Π΅Π΄Π΅Π½Π½ΡΠΉ ΠΏΡΠΈΠΌΠ΅Ρ Π²ΡΠ²Π΅Π΄Π΅ΠΌ ΠΏΡΡΠΌΠΎ Π·Π΄Π΅ΡΡ:
ΠΡΠ»ΠΈ ΠΌΡ Π² ΡΡΠ»ΠΎΠ²ΠΈΠΈ ΠΏΠ΅ΡΠ΅Π΄ ΠΊΠ°ΠΆΠ΄ΠΎΠΉ ΠΏΠ΅ΡΠ΅ΠΌΠ΅Π½Π½ΠΎΠΉ ΠΏΠΎΡΡΠ°Π²ΠΈΠΌ Π²ΠΎΡΠΊΠ»ΠΈΡΠ°ΡΠ΅Π»ΡΠ½ΡΠΉ Π·Π½Π°ΠΊ, ΡΠΎΠΌΡ ΡΠΌΠΎΠΆΠ΅Ρ ΠΏΡΠΎΠ²Π΅ΡΠΈΡΡ Π΅ΡΠ»ΠΈ Π²ΡΠ΅ ΠΏΠ΅ΡΠ΅ΠΌΠ΅Π½Π½ΡΠ΅ Π½Π΅ ΡΡΡΠ΅ΡΡΠ²ΡΠ΅Ρ
ΠΠ΄Π΅ΡΡ ΡΠ΄Π΅Π»Π°Π΅ΠΌ, ΡΡΠΎ ΠΏΠ΅ΡΠ΅ΠΌΠ΅Π½Π½ΡΠ΅ Π²ΡΠ΅ Π½Π΅ ΡΡΡΠ΅ΡΡΠ²ΡΡΡ!
ΠΠ΅ΡΠΊΠΎΠ»ΡΠΊΠΎ ΠΏΡΠΎΠ²Π΅ΡΠΎΠΊ ΡΡΡΠ΅ΡΡΠ²ΠΎΠ²Π°Π½ΠΈΡ ΠΎΠ΄Π½ΠΎΠΉ ΠΈΠ»ΠΈ Π²ΡΠ΅Ρ ΠΏΠ΅ΡΠ΅ΠΌΠ΅Π½Π½ΡΡ
ΠΠΎΠ·ΡΠΌΠ΅ΠΌ ΠΏΡΠΈΠΌΠ΅Ρ ΠΈΠ· ΡΠ°ΠΌΠΎΠ³ΠΎ Π²Π΅ΡΡ Π½Π΅Π³ΠΎ ΠΏΡΠ½ΠΊΡΠ°, Π³Π΄Π΅ ΠΎΠ΄Π½Π° ΠΏΠ΅ΡΠ΅ΠΌΠ΅Π½Π½Π°Ρ Π½Π΅ ΡΡΡΠ΅ΡΡΠ²ΡΠ΅Ρ. Π½Π΅ Π±ΡΠ΄Π΅ΠΌ ΠΏΠΎΠ²ΡΠΎΡΡΡΡ ΠΏΠ΅ΡΠ΅ΠΌΠ΅Π½Π½ΡΠ΅, ΡΠΌ.ΠΏΠ΅ΡΠ²ΡΠΉ ΠΏΡΠ½ΠΊΡ!
Π ΡΠ°ΠΊΠΆΠ΅ Π²ΡΠ²Π΅Π΄Π΅ΠΌ ΡΠ΅Π·ΡΠ»ΡΡΠ°Ρ ΠΏΡΡΠΌΠΎ Π·Π΄Π΅ΡΡ:
ΠΡΠΏΠΎΠ»ΡΠ·ΡΠ΅ΠΌ ΡΡΠΎ ΠΆΠ΅ ΡΡΠ»ΠΎΠ²ΠΈΠ΅, Π½ΠΎ Π½ΠΈΠΊΠ°ΠΊΠ°Ρ ΠΏΠ΅ΡΠ΅ΠΌΠ΅Π½Π½Π°Ρ Π·Π°ΡΠ°Π½Π΅Π΅ Π½Π΅ Π±ΡΠ΄Π΅Ρ ΡΡΡΠ΅ΡΡΠ²ΠΎΠ²Π°ΡΡ.
ΠΠΎ ΠΊΠ°ΠΊ ΡΡΠ°Π±ΠΎΡΠ°Π΅Ρ Π½Π΅ΡΠΊΠΎΠ»ΡΠΊΠΎ ΠΏΡΠΎΠ²Π΅ΡΠΎΠΊ Π΅ΡΠ»ΠΈ Π²ΡΠ΅ ΠΏΠ΅ΡΠ΅ΠΌΠ΅Π½Π½ΡΠ΅ Π½Π΅ Π±ΡΠ΄ΡΡ ΡΡΡΠ΅ΡΡΠ²ΠΎΠ²Π°ΡΡ.
Π ΡΠ°ΠΊΠΆΠ΅ Π²ΡΠ²Π΅Π΄Π΅ΠΌ ΡΠ΅Π·ΡΠ»ΡΡΠ°Ρ ΠΏΡΡΠΌΠΎ Π·Π΄Π΅ΡΡ:
ΠΠ΅ΡΠΊΠΎΠ»ΡΠΊΠΎ ΠΊΠ°ΡΠΊΠ°Π΄Π½ΡΡ ΠΏΡΠΎΠ²Π΅ΡΠΎΠΊ Π΄ΠΎ ΠΏΠ΅ΡΠ²ΠΎΠ³ΠΎ ΡΡΡΠ΅ΡΡΠ²ΠΎΠ²Π°Π½ΠΈΡ ΠΏΠ΅ΡΠ΅ΠΌΠ΅Π½Π½ΠΎΠΉ
ΠΡΠ΅Π΄ΠΏΠΎΠ»ΠΎΠΆΠΈΠΌ. ΡΡΠΎ Ρ Π½Π°Ρ Π΅ΡΡΡ Π½Π΅ΡΠΊΠΎΠ»ΡΠΊΠΎ ΠΏΠ΅ΡΠ΅ΠΌΠ΅Π½Π½ΡΡ , ΠΎΠΏΡΡΡ Π²ΠΎΠ·ΡΠΌΠ΅ΠΌ ΠΏΡΠΈΠΌΠ΅Ρ ΠΈΠ· ΠΏΠ΅ΡΠ²ΠΎΠ³ΠΎ ΠΏΡΠ½ΠΊΡΠ°. ΡΠ°Π· ΡΠΆ ΡΠ°ΠΌ Π½Π΅ ΡΡΡΠ΅ΡΡΠ²ΡΠ΅Ρ ΠΎΠ΄Π½Π° ΠΏΠ΅ΡΠ΅ΠΌΠ΅Π½Π½Π°Ρ. Π΅Ρ ΠΏΠΎΡΡΠ°Π²ΠΈΠΌ Π½Π° ΠΏΠ΅ΡΠ²ΠΎΠ΅ ΡΡΠ»ΠΎΠ²ΠΈΠ΅, Π° Π²ΡΠ΅ ΠΎΡΡΠ°Π»ΡΠ½ΡΠ΅ Π½ΠΈΠΆΠ΅.
Π Π΅ΡΡΠ΅ΡΡΠ²Π΅Π½Π½ΠΎ ΠΌΡ Π²ΡΠ²Π΅Π΄Π΅ΠΌ ΡΠ΅Π·ΡΠ»ΡΡΠ°Ρ Π·Π΄Π΅ΡΡ:
ΠΡΠΎΠ²Π΅ΡΠΊΠ° ΠΏΠ΅ΡΠ΅ΠΌΠ΅Π½Π½ΡΡ ΠΏΠΎ ΠΎΠ΄Π½ΠΎΠΉ.
ΠΠ΅ΡΠΊΠΎΠ»ΡΠΊΠΎ ΠΏΡΠΎΠ²Π΅ΡΠΎΠΊ ΠΈΡΠΏΠΎΠ»ΡΠ·ΡΡ ΠΌΠ°ΡΡΠΈΠ²
ΠΠΎΠ·ΡΠΌΠ΅ΠΌ ΠΎΠΏΡΡΡ, Π²ΡΠ΅ ΠΏΠ΅ΡΠ΅ΠΌΠ΅Π½Π½ΡΠ΅ ΠΈΠ· ΠΏΡΠ½ΠΊΡΠ° 1 ΠΈ Π·Π°Π½Π΅ΡΠ΅ΠΌ Π²ΡΠ΅ Π½Π°ΡΠΈ ΠΏΠ΅ΡΠ΅ΠΌΠ΅Π½Π½ΡΠ΅ Π² ΠΌΠ°ΡΡΠΈΠ² :
ΠΠΎΠ»ΡΡΠΈΠΌ ΠΊΠ»ΡΡΠΈ Π² ΠΌΠ°ΡΡΠΈΠ²:
ΠΡΠΎΠ²Π΅ΡΠΈΠΌ Π² ΡΠΈΠΊΠ»Π΅ ΡΡΡΠ΅ΡΡΠ²ΠΎΠ²Π°Π½ΠΈΠ΅ ΠΏΠ΅ΡΠ΅ΠΌΠ΅Π½Π½ΠΎΠΉ:
ΠΠ΅ΡΠΊΠΎΠ»ΡΠΊΠΎ ΠΏΡΠΎΠ²Π΅ΡΠΎΠΊ Π² ΡΡΠ°ΡΠΎΠΌ ΠΊΠΎΠ½ΡΠ΅Π½ΡΠ΅
ΠΡΡ ΠΈΠ² Ρ ΡΡΠΈΠΌ Π²Π°ΡΠΈΠ°Π½ΡΠΎΠΌ Π½Π΅ ΡΡΠ°Π» ΡΠ΄Π°Π»ΡΡΡ.
Π‘ΠΎΠΎΠ±ΡΠ΅Π½ΠΈΠ΅ ΡΠΈΡΡΠ΅ΠΌΡ ΠΊΠΎΠΌΠΌΠ΅Π½ΡΠΈΡΠΎΠ²Π°Π½ΠΈΡ :
Π€ΠΎΡΠΌΠ° ΠΏΠΎΠΊΠ° Π΄ΠΎΡΡΡΠΏΠ½Π° ΡΠΎΠ»ΡΠΊΠΎ Π°Π΄ΠΌΠΈΠ½Ρ. ΡΠΊΠΎΡΠΎ Π²ΡΠ΅ Π·Π°ΡΠ°Π±ΠΎΡΠ°Π΅Ρ. Π½Π°Π΄Π΅ΡΡΡ.
function_exists
(PHP 4, PHP 5, PHP 7, PHP 8)
ΠΠΏΠΈΡΠ°Π½ΠΈΠ΅
Π‘ΠΏΠΈΡΠΎΠΊ ΠΏΠ°ΡΠ°ΠΌΠ΅ΡΡΠΎΠ²
ΠΠΌΡ ΡΡΠ½ΠΊΡΠΈΠΈ Π² Π²ΠΈΠ΄Π΅ ΡΡΡΠΎΠΊΠΈ.
ΠΠΎΠ·Π²ΡΠ°ΡΠ°Π΅ΠΌΡΠ΅ Π·Π½Π°ΡΠ΅Π½ΠΈΡ
ΠΡΠΈΠΌΠ΅ΡΡ
ΠΡΠΈΠΌΠ΅Ρ #1 ΠΡΠΈΠΌΠ΅Ρ ΠΈΡΠΏΠΎΠ»ΡΠ·ΠΎΠ²Π°Π½ΠΈΡ function_exists()
ΠΡΠΈΠΌΠ΅ΡΠ°Π½ΠΈΡ
ΠΠ±ΡΠ°ΡΠΈΡΠ΅ Π²Π½ΠΈΠΌΠ°Π½ΠΈΠ΅, ΡΡΠΎ Π½Π°Π·Π²Π°Π½ΠΈΠ΅ ΡΡΠ½ΠΊΡΠΈΠΈ ΠΌΠΎΠΆΠ΅Ρ ΠΏΡΠΈΡΡΡΡΡΠ²ΠΎΠ²Π°ΡΡ, Π΄Π°ΠΆΠ΅ Π΅ΡΠ»ΠΈ ΡΠ°ΠΌΡ ΡΡΠ½ΠΊΡΠΈΡ Π½Π΅Π²ΠΎΠ·ΠΌΠΎΠΆΠ½ΠΎ ΠΈΡΠΏΠΎΠ»ΡΠ·ΠΎΠ²Π°ΡΡ ΠΈΠ·-Π·Π° Π½Π°ΡΡΡΠΎΠ΅ΠΊ ΠΊΠΎΠ½ΡΠΈΠ³ΡΡΠ°ΡΠΈΠΈ ΠΈΠ»ΠΈ ΠΎΠΏΡΠΈΠΉ ΠΊΠΎΠΌΠΏΠΈΠ»ΡΡΠΈΠΈ (Π½Π°ΠΏΡΠΈΠΌΠ΅Ρ, ΠΊΠ°ΠΊ Π΄Π»Ρ ΡΡΠ½ΠΊΡΠΈΠΉ image).
Π‘ΠΌΠΎΡΡΠΈΡΠ΅ ΡΠ°ΠΊΠΆΠ΅
User Contributed Notes 23 notes
You can use this function to conditionally define functions, see: http://php.net/manual/en/functions.user-defined.php
For instance WordPress uses it to make functions «pluggable.» If a plugin has already defined a pluggable function, then the WP code knows not to try to redefine it.
But function_exists() will always return true unless you wrap any later function definition in a conditional clause, like if()<. >. This is a subtle matter in PHP parsing. Some examples:
if ( function_exists ( ‘foo’ )) <
print «foo defined\\n» ;
> else <
print «foo not defined\\n» ;
>
function foo () <>
if ( function_exists ( ‘bar’ )) <
print «bar defined\\n» ;
> else <
print «defining bar\\n» ;
function bar () <>
>
print «calling bar\\n» ;
bar (); // ok to call function conditionally defined earlier
print «calling baz\\n» ;
baz (); // ok to call function unconditionally defined later
function baz () <>
qux (); // NOT ok to call function conditionally defined later
if (! function_exists ( ‘qux’ )) <
function qux () <>
>
?>
Prints:
foo defined
defining bar
calling bar
calling baz
PHP Fatal error: Call to undefined function qux()
Any oddities are probably due to the order in which you include/require files.
It should be noted that the function_exists check is not relative to the root namespace. This means that the namespace should be appended to the check:
PHP supports nested function based on certain criteria.
Please look over the code.
function Audio()
<
echo «Plugged Audo 5.1:
«;
function Volume()
<
echo «Volume controls:
«;
function Equalizer()
<
echo «Equalize Bands:
«;
>
>
>
//Call to nested functions
Audio();
Volume();
Equalizer();
if(function_exists(‘Volume’)):
echo «TRUE»;
else:
echo «FALSE»;
endif;
Case 1: //Result :Works Well
———
Audio();
Volume();
Equalizer();
Case 2: //Results Notice Error. Root function Audio must be called first.
———
Volume();
Case 3: //Results Error. Root function Volume must be called.
———
Audio();
Equalizer();
Note :
The nested function should be called based on their order used.
In our example when Audio is not called and instantly when we try to call Volume puts under error.
Even though there is an possibility to use nested functions in PHP. It looks overhead to do so. Better to avoid in logical ground of script.
Tested on PHP 5.5.32
This is not going to go down as you might expect it should (even if you play smart and require/include_once):
if( function_exists ( ‘my_function’ ))
<
throw new Exception ( «‘my_function’ is already defined!» );
>
function my_function ()
<
// Do the work here
>
?>
This, however does work:
Thought this might save someone a few minutes of debugging time.
function_exists will return false for functions disabled with the disable_functions ini directive. However those functions are still declared so trying to define them yourself will fail.
Functions within a function are better off as anonymous returns from create_function(), unless you want to be able to call it elsewhere.
However, I have used this in skinning: I use alert_box() to display certain errors, like a faulty SQL query. This simply calls display_alert(), which is defined in my skin scripts. However, alert_box() is sometimes called before I know which skin to load, so it has its own functionality which it uses if function_exists(‘display_alert’) returns false.
If you use suhosin.executor.func.blacklist instead of disabled_functions in your php.ini, function_exists will return true for a disabled function. I used this to have the same beahviour with suhosin.executor.func.blacklist and disabled_functions:
To prevent direct calls to included files i use the following technique.
In the main file create an empty function with a random name. Like so:
function hjudejdjiwe () < return true ; >
?>
Then check for the existence of this function within your include:
if (! function_exists ( ‘hjudejdjiwe’ )) < die( '!' ); >
?>
Simple but effective.
i was wondering whether is_callable or function exists is faster when checking class methods.
my results when doing each operation 10000 times with a simple test class were the following:
is_callable: 0.28671383857727 seconds
function_exists: 0.14569997787476 seconds
(following tests have proved this to be true).
thus you can see, function_exists is twice as fast as is_callable.
The confusion expressed in some of the submissions here arise because functions declared outside conditional blocks are defined as the code is loaded and are thus callable and exist wherever in the code they are declared, whereas those declared inside a condition block are not defined until that block is executed. Thus:
echo foo();
function foo()
however because those inside a conditional are defined as they are encountered during code execution
yields: Fatal error: Uncaught Error: Call to undefined function foo()
I would like to comment on the following post:
A note of caution: function_exists() appears to be case-insensitive (at least as of PHP 4.3.8). e.g.:
function MyCasedFunction () <
return true ;
>
// Will return true, even though casing is «wrong»
if ( function_exists ( «mYcAsEdFuNcTiOn» ))
echo «I see it!» ;
?>
I believe that function calls itself are case insensitve, so this function is returning a valid truth. PHP doesn’t care about cases.
function B () <
function C () <
function D ()<>
>
>
IsFunctionExist ( ‘A’ );
IsFunctionExist ( ‘B’ );
IsFunctionExist ( ‘C’ );
IsFunctionExist ( ‘D’ );
I, too, was wondering whether is_callable or function exists is faster when checking class methods. So, I setup the following test:
class test
<
function test ()
<
return true ;
>
>
is_callable = TRUE, function_exists = FALSE
Did 10000 is_callables in 0.0640790462494 seconds
Did 10000 function_exists in 0.0304429531097 seconds
So the fact that function_exists is twice as fast is slightly over shadowed by the fact that it doesn’t work on class methods, at least not as far as I can tell.
This can be used to conditionally define a user function. In this sense, it can act as a sort of inline include_once().
For example, suppose you have a function A that calls function B. B is only used inside function A and is never called from anywhere else in the script. It’s logical (and perfectly legal in PHP) to define B inside of A’s definition, like so:
I stumbled over the same problem as «eddiec» (users not able or not willing to use «_once»-suffixes).
A possible alternative explanation for the behavior:
If a file is included, it is possibly parsed every include-time.(?)
While parsing, every function in global scope is tried to register. THIS gets wrong, when multiple times included, and it produces an error.
If functions are defined within block scopes, their registration seems to be delayed until execution of such a block. Thus, not the function «function_exists» functions wrong, but simply the philosophy of the interpreter produces such results.
Thus, the same effect can be achieved by simply putting block braces around the contents of an include_once file:
if (function_exists(‘function_in_question’)) return;
<
function function_in_question(. )
<
.
>
. other stuff
>
. which is equivalent to.
if (!function_exists(‘function_in_question’))
<
function function_in_question(. )
<
.
>
. other stuff
>
// If you want to chack if the function is enabled or disable in php.ini you can use this function:
function_exists returns false on NULL and empty string:
if ( function_exists ( » )) <
echo «empty string function exists\n» ;
>
if ( function_exists ( NULL )) <
echo «NULL function exists\n» ;
>
?>
Neither of the echo statements happen when I run this.
Note that function_exists will return TRUE in the following situation, presumably because the function «testfunc» was defined when the script was PARSED/ITERPRETED, before the function_exists call was made at RUNTIME:
if ( function_exists ( ‘testfunc’ )) return;
function testfunc () < >
?>
So, this construction is not useful for preventing testfunc from being multiply defined if the script is muliply included or required.
However, the following construction DOES work to prevent multiple defines of testfunc:
if (! function_exists ( ‘testfunc’ )) <
function testfunc () < >
>
?>
CONTRAST this with similar uses of defined() which is completely runtime evaluated. These both work:
function_exists() does not cache its query internally.
by executing the following code
There is a 0.16878 seconds improvement, when you use static array to cache methods existence.
to bob at thethirdshift dot net
regarding is_callable vs function_exists.
using your code
is_callable = TRUE, function_exists = FALSE
Did 10000 is_callables in 0.0443360805511 seconds
Did 10000 function_exists in 0.0111110210419 seconds
then we replace
is_callable(array(‘test’,’test’));
with
$callarray = array(‘test’,’test’); // place this outside for-loop
is_callable($callarray);
is_callable = TRUE, function_exists = FALSE
Did 10000 is_callables in 0.0314660072327 seconds
Did 10000 function_exists in 0.0120670795441 seconds
then we replace
is_callable(array(‘test’,’test’));
with
is_callable(‘test’,’test’);
is_callable = TRUE, function_exists = FALSE
Did 10000 is_callables in 0.00991606712341 seconds
Did 10000 function_exists in 0.0113790035248 seconds
I hope you can see that loop-testing functions is not so simple. π
ΠΠ°ΠΊ ΠΏΡΠΎΠ²Π΅ΡΠΈΡΡ Π½Π°Π»ΠΈΡΠΈΠ΅ ΠΏΠ΅ΡΠ΅ΠΌΠ΅Π½Π½ΠΎΠΉ Π² POST?
Π£ ΠΌΠ΅Π½Ρ Π΅ΡΡΡ ΡΠΎΡΠΌΠ° ΠΈ ajax ΠΊΠΎΠ΄ ΠΊΠΎΡΠΎΡΠ°Ρ Π²ΡΠ΅ ΡΡΠΎ ΠΎΡΠΏΡΠ°Π²Π»ΡΠ΅Ρ, Π½ΠΎ ΠΏΡΠΈ ΠΎΡΠΏΡΠ°Π²ΠΊΠ΅ Π΄ΠΎΠ»ΠΆΠ½Π° Π±ΡΡΡ ΠΏΡΠΎΠ²Π΅ΡΠΊΠ° Π½Π° Π½Π°Π»ΠΈΡΠΈΠ΅ Π΄Π°Π½Π½ΡΡ
Π² ΠΏΠΎΠ»ΡΡ
.
ΠΡΠΎΠ±Π»Π΅ΠΌΠ° Π² ΡΠΎΠΌ, ΡΡΠΎ ΠΏΡΠΎΠ²Π΅ΡΠΊΠ° isset(), empty() Π½Π΅ ΡΠ°Π±ΠΎΡΠ°Π΅Ρ!(
ΠΠ·Π½Π°ΡΠ°Π»ΡΠ½ΠΎ Ρ Π΄Π΅Π»Π°Π» ΡΠ°ΠΊ ΡΡΠΎ ΠΏΠ΅ΡΠ΅ΠΌΠ΅Π½Π½ΡΠ΅ ΠΎΠ±ΡΡΠ²Π»ΡΠ»ΠΈΡΡ ΡΠΎΠ»ΡΠΊΠΎ ΠΏΠΎΡΠ»Π΅ ΠΏΡΠΎΠ²Π΅ΡΠΊΠΈ.
Π ΡΠ΅ΠΌ ΠΏΡΠΎΠ±Π»Π΅ΠΌΠ° ΠΈ ΠΏΠΎΡΠ΅ΠΌΡ ΡΠ°ΠΊ ΠΏΡΠΎΠΈΡΡ
ΠΎΠ΄ΠΈΡ?
PHP ΠΊΠΎΠ΄ ΡΠΊΠ°Π·Π°Π½ Π² ΡΠ΅ΠΊΡΡΠ΅ Π²ΠΎΠΏΡΠΎΡΠ°. ΠΡΠΎ Π²ΡΠ΅ ΡΡΠΎ Π΅ΡΡΡ.
prepared statements ΠΈ Π² ΠΊΠ°ΠΊΠΎΠ΅-Π»ΠΈΠ±ΠΎ ΡΠΊΡΠ°Π½ΠΈΡΠΎΠ²Π°Π½ΠΈΠ΅ ΠΈΠ»ΠΈ ΠΏΡΠΎΠ²Π΅ΡΠΊΡ ΠΏΠΎΠ»ΡΠ·ΠΎΠ²Π°ΡΠ΅Π»ΡΡΠΊΠΈΡ Π΄Π°Π½Π½ΡΡ
Ρ Π½Π΅ ΡΠ³Π»ΡΠ±Π»ΡΠ»ΡΡ. Π Π°Π· ΠΈΠ½ΡΠ΅ΡΠ΅ΡΡΠ΅ΡΠ΅ΡΡ.
ΠΠ° ΠΊΠ°ΠΊΠΎΠ³ΠΎ Π»ΠΈΠ±ΠΎ ΡΠ΅Π»ΠΎΠ²Π΅ΠΊΠ° ΠΌΠΎΠΆΠ΅ΡΠ΅ Π½Π΅ ΠΏΠ΅ΡΠ΅ΠΆΠΈΠ²Π°ΡΡ, Π²ΡΠ΅ ΡΡΠΈ Π²ΠΎΠΏΡΠΎΡΡ Π² ΡΠ΅Π»ΡΡ ΡΠ²ΠΎΠ΅Π³ΠΎ ΠΎΠ±ΡΡΠ΅Π½ΠΈΡ. ΠΠΎΡΡΡΠ°Π΄Π°Π²ΡΠΈΡ Π½Π΅ΡΡ. Π Π°Π΄ΡΠΊΠΈΠΉ ΠΏΡ ΠΏ ΠΊΠΎΠ΄ Π½Π°Ρ ΠΎΠ΄ΠΈΡΡΡ ΡΠΎΠ»ΡΠΊΠΎ Π½Π° ΠΌΠΎΠ΅ΠΉ Π»ΠΎΠΊΠ°Π»ΠΊΠ΅, ΠΈ ΠΏΡΡΠΌΠ°Ρ ΠΏΠ΅ΡΠ΅Π΄Π°ΡΠ° ΠΏΠΎΡΡΠ° Π² Π·Π°ΠΏΡΠΎΡ ΠΠ ΠΏΡΠ΅Π΄ΡΠ΅ΡΡΠ²ΡΠ΅Ρ, ΡΠΊΠ°ΠΆΠ΅ΠΌ ΡΠ°ΠΊ, Π‘ΠΠΠ Π’Ρ ΡΠΎΠ»ΡΠΊΠΎ ΠΌΠΎΠ΅ΠΉ ΠΠ.
ΠΎΠΏΠ΅ΡΠ°ΡΠΊΠ° Π² ΠΈΠΌΠ΅Π½ΠΈ ΠΏΠ΅ΡΠ΅ΠΌΠ΅Π½Π½ΠΎΠΉ. ΠΠΎΡΠ΅ΡΡΠ½Π° ΠΎΠ΄Π½Π° Π±ΡΠΊΠ²Π° «r»
Π ΠΏΡΠΎΠ²Π΅ΡΠΊΠ° ΡΠ΅ΡΠ΅Π· isset() + empty()
ΡΠΊΠ²ΠΈΠ²Π°Π»Π΅Π½ΡΠ½ΠΎ ΡΠ°ΠΊΠΎΠΌΡ ΠΊΠΎΠ΄Ρ
Π ΠΏΡΠΎΠ²Π΅ΡΡΡΡ Π½Π°Π»ΠΈΡΠΈΠ΅ ΠΏΠ΅ΡΠ΅ΠΌΠ΅Π½Π½ΠΎΠΉ Π½Π΅ Π½ΡΠΆΠ½ΠΎ. ΠΠ΅ΡΠ΅ΠΌΠ΅Π½Π½Π°Ρ Π² ΠΏΠΎΡΡΠ΅ Π²ΡΠ΅Π³Π΄Π° Π΅ΡΡΡ. ΠΡΠ»ΠΈ Π½Π°Π΄ΠΎ ΠΏΡΠΎΠ²Π΅ΡΠΈΡΡ Π½Π° ΠΏΡΡΡΠΎΡΡ, ΡΠΎ ΡΡΠΎ Π΄Π΅Π»Π°Π΅ΡΡΡ ΡΠΎΠ²ΡΠ΅ΠΌ Π΄ΡΡΠ³ΠΈΠΌΠΈ ΡΡΠ½ΠΊΡΠΈΡΠΌΠΈ.