PHP DEĞİŞKENLER
Bugün sizlere Php Değişkenler konusunda Değişken Temellerinden bahsedeceğim.
Değişken Temelleri
PHP Değişkenler değişken adını ardından dolar işareti ile temsil edilir. Değişken adı küçük harf duyarlıdır.
Değişken isimleri PHP diğer etiketiyle aynı kurallara uyun. Geçerli bir değişken adı harf, sayı veya alt çizgi herhangi bir sayı ardından bir harf veya alt çizgi ile başlar. Düzenli ifade olarak, şu şekilde ifade edilebilir: '[a-zA-Z_ \ x7f-\ xff] [a-zA-Z0-9_ \ x7f-\ xff] *'
Not: Burada kastedilen, bir harf az, AZ, ve 127 ile 255 (0x7f-0xff) aracılığıyla bayt.
Not: Bu atanamaz özel bir değişkendir $.
<?php
$var = 'Bob' ; $Var = 'Joe' ;
echo " $var , $Var " ; // outputs "Bob, Joe"
$ 4site = 'not yet' ; // invalid; starts with a number $_4site = 'not yet' ; // valid; starts with an underscore $täyte = 'mansikka' ; // valid; 'ä' is (Extended) ASCII 228. ?>
Varsayılan olarak, değişkenler her zaman değerleriyle atanır. Bir değişkene bir ifade atadığınızda, demek ki, orijinal ifadenin tüm değeri, hedef değişken olarak kopyalanır. Bunun anlamı, örneğin, başka bir değişkenin değerini atadıktan sonra, bu değişkenlerin bir değişen diğer üzerinde bir etkisi olacak.
PHP değişkenlere değer atamak için başka bir yol sunar referans atamak . Bu yeni değişken sadece orijinal değişkenin (diğer bir deyişle, "işaret" veya "için bir takma ad olur") anlamına gelir. Yeni bir değişken için değişiklikler aslını da etkiler, ve tersi.
Referans ile atamak için, sadece (kaynak değişken) atanan olan değişkenin başına ve imi (&) ekleyin. Örneğin, aşağıdaki kod parçası iki kez 'Benim adım Mustafa':
<?php
$foo = 'Bob' ; // Assign the value 'Bob' to $foo $bar = & $foo ; // Reference $foo via $bar. $bar = "My name is $bar " ; // Alter $bar... echo $bar ;
echo $foo ; // $foo is altered too. ?>
Unutulmaması gereken önemli bir şey, sadece adında değişkenler referans tarafından atanabilir olmasıdır.
<?php
$foo = 25 ; $bar = & $foo ; // This is a valid assignment. $bar = &( 24 * 7 ); // Invalid; references an unnamed expression.
function test ()
{
return 25 ;
}
$bar = & test (); // Invalid. ?>
Ancak çok iyi bir uygulama PHP değişkenlerini başlatmak için gerekli değildir. Başlatılmamış değişkenlerin kullanıldıkları bağlama türlerine bağlı olarak varsayılan bir değer var - varsayılan boolean FALSE
, tam sayılar ve sıfıra yüzer varsayılan, dizeleri (örneğin kullanılan eko ) boş bir dize olarak ayarlayın ve diziler boş haline getirilir
Başlatılmamış değişkenlere Örnek 1 Varsayılan değerler
<?php // Unset AND unreferenced (no use context) variable; outputs NULL var_dump ( $unset_var );
// Boolean usage; outputs 'false' (See ternary operators for more on this syntax) echo( $unset_bool ? "true\n" : "false\n" );
// String usage; outputs 'string(3) "abc"' $unset_str .= 'abc' ; var_dump ( $unset_str );
// Integer usage; outputs 'int(25)' $unset_int += 25 ; // 0 + 25 => 25 var_dump ( $unset_int );
// Float/double usage; outputs 'float(1.25)' $unset_float += 1.25 ; var_dump ( $unset_float );
// Array usage; outputs array(1) { [3]=> string(3) "def" } $unset_arr [ 3 ] = "def" ; // array() + array(3 => "def") => array(3 => "def") var_dump ( $unset_arr );
// Object usage; creates new stdClass object (see http://www.php.net/manual/en/reserved.classes.php)
// Outputs: object(stdClass)#1 (1) { ["foo"]=> string(3) "bar" } $unset_obj -> foo = 'bar' ; var_dump ( $unset_obj ); ?>
Başlatılmamış bir değişkenin varsayılan değeri dayanarak aynı değişken isminin kullanıldığı bir dosyayı dahil olmak durumu sorunludur. Ayrıca büyük bir olan güvenlik riski ile register_globals açık. E_NOTICE seviyesinde bir hata başlatılmamış değişkenler çalışma durumunda verilir, ancak başlatılmamış bir diziye eleman eklenmesi durumunda. değil isset () dil oluşumu tespit etmek için kullanılabilecek bir eğer değişken zaten başlatılmış.
2 yıl önce
This page should include a note on variable lifecycle:
Before a variable is used, it has no existence. It is unset. It is possible to check if a variable doesn't exist by using isset(). This returns true provided the variable exists and isn't set to null. With the exception of null, the value a variable holds plays no part in determining whether a variable is set.
Setting an existing variable to null is a way of unsetting a variable. Another way is variables may be destroyed by using the unset() construct.
<?php print isset( $a ); // $a is not set. Prints false. (Or more accurately prints ''.) $b = 0 ; // isset($b) returns true (or more accurately '1') $c = array(); // isset($c) returns true $b = null ; // Now isset($b) returns false; unset( $c ); // Now isset($c) returns false; ?>
is_null() is an equivalent test to checking that isset() is false.
The first time that a variable is used in a scope, it's automatically created. After this isset is true. At the point at which it is created it also receives a type according to the context.
<?php
$a_bool = true ; // a boolean $a_str = 'foo' ; // a string ?>
If it is used without having been given a value then it is uninitalized and it receives the default value for the type. The default values are the _empty_ values. Eg Booleans default to FALSE, integers and floats default to zero, strings to the empty string '', arrays to the empty array.
A variable can be tested for emptiness using empty();
<?php
$a = 0 ; //This isset, but is empty ?>
Unset variables are also empty.
<?php empty( $vessel ); // returns true. Also $vessel is unset. ?>
Everything above applies to array elements too.
<?php
$item = array(); //Now isset($item) returns true. But isset($item['unicorn']) is false.
//empty($item) is true, and so is empty($item['unicorn']
$item [ 'unicorn' ] = '' ; //Now isset($item['unicorn']) is true. And empty($item) is false.
//But empty($item['unicorn']) is still true;
$item [ 'unicorn' ] = 'Pink unicorn' ; //isset($item['unicorn']) is still true. And empty($item) is still false.
//But now empty($item['unicorn']) is false; ?>
For arrays, this is important because accessing a non-existent array item can trigger errors; you may want to test arrays and array items for existence with isset before using them.
sesleri nokta com megan
1 yıl önce
"Note: $this is a special variable that can't be assigned."
While the PHP runtime generates an error if you directly assign $this in code, it doesn't for $$name when name is 'this'.
<?php
$this = 'text' ; // error
$name = 'this' ;
$ $name = 'text' ; // sets $this to 'text'
Edoxile
3 yıl önce
When wanting to switch two variables from content, you can use the XOR operator:
<?PHP
$a = 5 ; $b = 3 ;
//Please mind the order of these, as it's important for the outcome.
$a ^= $b ; $b ^= $a ; $a ^= $b ;
echo $a . PHP_EOL . $b ; /* prints:
3
5
*/ ?>
This will also work on strings, but it won't work on arrays and objects, so for them you'll have to use the serialize() function before the operation, and the unserialize() function after.
pu nokta t-com nokta saatte maurizio nokta Domba
2 yıl önce
If you need to check user entered value for a proper PHP variable naming convention you need to add ^ to the above regular expression so that the regular expression should be '^[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*'.
Example
<?php
$name = "20011aa" ;
if(! preg_match ( '/[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*/' , $name ))
echo $name . ' is not a valid PHP variable name' ;
else
echo $name . ' is valid PHP variable name' ; ?>
Outputs: 2011aa is valid PHP variable name
but
<?php
$name = "20011aa" ;
if(! preg_match ( '/^[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*/' , $name ))
echo $name . ' is not a valid PHP variable name' ;
else
echo $name . ' is valid PHP variable name' ; ?>
Outputs: 2011aa is not a valid PHP variable name
PHP Değişkenler değişken adını ardından dolar işareti ile temsil edilir. Değişken adı küçük harf duyarlıdır.
Değişken isimleri PHP diğer etiketiyle aynı kurallara uyun. Geçerli bir değişken adı harf, sayı veya alt çizgi herhangi bir sayı ardından bir harf veya alt çizgi ile başlar. Düzenli ifade olarak, şu şekilde ifade edilebilir: '[a-zA-Z_ \ x7f-\ xff] [a-zA-Z0-9_ \ x7f-\ xff] *'
Not: Burada kastedilen, bir harf az, AZ, ve 127 ile 255 (0x7f-0xff) aracılığıyla bayt.
Not: Bu atanamaz özel bir değişkendir $.
<?php
$var = 'Bob' ; $Var = 'Joe' ;
echo " $var , $Var " ; // outputs "Bob, Joe"
$ 4site = 'not yet' ; // invalid; starts with a number $_4site = 'not yet' ; // valid; starts with an underscore $täyte = 'mansikka' ; // valid; 'ä' is (Extended) ASCII 228. ?>
Varsayılan olarak, değişkenler her zaman değerleriyle atanır. Bir değişkene bir ifade atadığınızda, demek ki, orijinal ifadenin tüm değeri, hedef değişken olarak kopyalanır. Bunun anlamı, örneğin, başka bir değişkenin değerini atadıktan sonra, bu değişkenlerin bir değişen diğer üzerinde bir etkisi olacak.
PHP değişkenlere değer atamak için başka bir yol sunar referans atamak . Bu yeni değişken sadece orijinal değişkenin (diğer bir deyişle, "işaret" veya "için bir takma ad olur") anlamına gelir. Yeni bir değişken için değişiklikler aslını da etkiler, ve tersi.
Referans ile atamak için, sadece (kaynak değişken) atanan olan değişkenin başına ve imi (&) ekleyin. Örneğin, aşağıdaki kod parçası iki kez 'Benim adım Mustafa':
<?php
$foo = 'Bob' ; // Assign the value 'Bob' to $foo $bar = & $foo ; // Reference $foo via $bar. $bar = "My name is $bar " ; // Alter $bar... echo $bar ;
echo $foo ; // $foo is altered too. ?>
Unutulmaması gereken önemli bir şey, sadece adında değişkenler referans tarafından atanabilir olmasıdır.
<?php
$foo = 25 ; $bar = & $foo ; // This is a valid assignment. $bar = &( 24 * 7 ); // Invalid; references an unnamed expression.
function test ()
{
return 25 ;
}
$bar = & test (); // Invalid. ?>
Ancak çok iyi bir uygulama PHP değişkenlerini başlatmak için gerekli değildir. Başlatılmamış değişkenlerin kullanıldıkları bağlama türlerine bağlı olarak varsayılan bir değer var - varsayılan boolean
FALSE
, tam sayılar ve sıfıra yüzer varsayılan, dizeleri (örneğin kullanılan eko ) boş bir dize olarak ayarlayın ve diziler boş haline getirilir
Başlatılmamış değişkenlere Örnek 1 Varsayılan değerler
<?php // Unset AND unreferenced (no use context) variable; outputs NULL var_dump ( $unset_var );
// Boolean usage; outputs 'false' (See ternary operators for more on this syntax) echo( $unset_bool ? "true\n" : "false\n" );
// String usage; outputs 'string(3) "abc"' $unset_str .= 'abc' ; var_dump ( $unset_str );
// Integer usage; outputs 'int(25)' $unset_int += 25 ; // 0 + 25 => 25 var_dump ( $unset_int );
// Float/double usage; outputs 'float(1.25)' $unset_float += 1.25 ; var_dump ( $unset_float );
// Array usage; outputs array(1) { [3]=> string(3) "def" } $unset_arr [ 3 ] = "def" ; // array() + array(3 => "def") => array(3 => "def") var_dump ( $unset_arr );
// Object usage; creates new stdClass object (see http://www.php.net/manual/en/reserved.classes.php)
// Outputs: object(stdClass)#1 (1) { ["foo"]=> string(3) "bar" } $unset_obj -> foo = 'bar' ; var_dump ( $unset_obj ); ?>
Başlatılmamış bir değişkenin varsayılan değeri dayanarak aynı değişken isminin kullanıldığı bir dosyayı dahil olmak durumu sorunludur. Ayrıca büyük bir olan güvenlik riski ile register_globals açık. E_NOTICE seviyesinde bir hata başlatılmamış değişkenler çalışma durumunda verilir, ancak başlatılmamış bir diziye eleman eklenmesi durumunda. değil isset () dil oluşumu tespit etmek için kullanılabilecek bir eğer değişken zaten başlatılmış.
2 yıl önce
This page should include a note on variable lifecycle:
Before a variable is used, it has no existence. It is unset. It is possible to check if a variable doesn't exist by using isset(). This returns true provided the variable exists and isn't set to null. With the exception of null, the value a variable holds plays no part in determining whether a variable is set.
Setting an existing variable to null is a way of unsetting a variable. Another way is variables may be destroyed by using the unset() construct.
<?php print isset( $a ); // $a is not set. Prints false. (Or more accurately prints ''.) $b = 0 ; // isset($b) returns true (or more accurately '1') $c = array(); // isset($c) returns true $b = null ; // Now isset($b) returns false; unset( $c ); // Now isset($c) returns false; ?>
is_null() is an equivalent test to checking that isset() is false.
The first time that a variable is used in a scope, it's automatically created. After this isset is true. At the point at which it is created it also receives a type according to the context.
<?php
$a_bool = true ; // a boolean $a_str = 'foo' ; // a string ?>
If it is used without having been given a value then it is uninitalized and it receives the default value for the type. The default values are the _empty_ values. Eg Booleans default to FALSE, integers and floats default to zero, strings to the empty string '', arrays to the empty array.
A variable can be tested for emptiness using empty();
<?php
$a = 0 ; //This isset, but is empty ?>
Unset variables are also empty.
<?php empty( $vessel ); // returns true. Also $vessel is unset. ?>
Everything above applies to array elements too.
<?php
$item = array(); //Now isset($item) returns true. But isset($item['unicorn']) is false.
//empty($item) is true, and so is empty($item['unicorn']
$item [ 'unicorn' ] = '' ; //Now isset($item['unicorn']) is true. And empty($item) is false.
//But empty($item['unicorn']) is still true;
$item [ 'unicorn' ] = 'Pink unicorn' ; //isset($item['unicorn']) is still true. And empty($item) is still false.
//But now empty($item['unicorn']) is false; ?>
For arrays, this is important because accessing a non-existent array item can trigger errors; you may want to test arrays and array items for existence with isset before using them.
sesleri nokta com megan
1 yıl önce
"Note: $this is a special variable that can't be assigned."
While the PHP runtime generates an error if you directly assign $this in code, it doesn't for $$name when name is 'this'.
<?php
$this = 'text' ; // error
$name = 'this' ;
$ $name = 'text' ; // sets $this to 'text'
Edoxile
3 yıl önce
When wanting to switch two variables from content, you can use the XOR operator:
<?PHP
$a = 5 ; $b = 3 ;
//Please mind the order of these, as it's important for the outcome.
$a ^= $b ; $b ^= $a ; $a ^= $b ;
echo $a . PHP_EOL . $b ; /* prints:
3
5
*/ ?>
This will also work on strings, but it won't work on arrays and objects, so for them you'll have to use the serialize() function before the operation, and the unserialize() function after.
pu nokta t-com nokta saatte maurizio nokta Domba
2 yıl önce
If you need to check user entered value for a proper PHP variable naming convention you need to add ^ to the above regular expression so that the regular expression should be '^[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*'.
Example
<?php
$name = "20011aa" ;
if(! preg_match ( '/[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*/' , $name ))
echo $name . ' is not a valid PHP variable name' ;
else
echo $name . ' is valid PHP variable name' ; ?>
Outputs: 2011aa is valid PHP variable name
but
<?php
$name = "20011aa" ;
if(! preg_match ( '/^[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*/' , $name ))
echo $name . ' is not a valid PHP variable name' ;
else
echo $name . ' is valid PHP variable name' ; ?>
Outputs: 2011aa is not a valid PHP variable name
0 yorum:
Yorum Gönder