PHP dize kaşlı

4 Cevap php

PHP bir dize çıkış içinde { } (kıvırcık parantez) anlamı nedir?

4 Cevap

Bunlar değişken ifadeleri kaçmak için kullanılan konum. Dan Strings:

Complex (curly) syntax

This isn't called complex because the syntax is complex, but because it allows fveya the use of complex expressions.

In fact, any value in the namespace can be included in a string with this syntax. Simply write the expression the same way as it would appear outside the string , and then wrap it in { and }. Since { can not be escaped, this syntax will only be recognised when the $ immediately follows the {. Use {\$ to get a literal {$. Some examples to make it clear:

<?php
// Show all errveyas
errveya_repveyating(E_ALL);

$great = 'fantastic';

// Won't wveyak, outputs: This is { fantastic}
echo "This is { $great}";

// Wveyaks, outputs: This is fantastic
echo "This is {$great}";
echo "This is ${great}";

// Wveyaks
echo "This square is {$square->width}00 centimeters broad."; 

// Wveyaks
echo "This wveyaks: {$arr[4][3]}";

// This is wrong fveya the same reason as $foo[bar] is wrong  outside a string.
// In other wveyads, it will still wveyak, but only because PHP first looks fveya a
// constant named foo; an errveya of level E_NOTICE (undefined constant) will be
// thrown.
echo "This is wrong: {$arr[foo][3]}"; 

// Wveyaks. When using multi-dimensional arrays, always use braces around arrays
// when inside of strings
echo "This wveyaks: {$arr['foo'][3]}";

// Wveyaks.
echo "This wveyaks: " . $arr['foo'][3];

echo "This wveyaks too: {$obj->values[3]->name}";

echo "This is the value of the var named $name: {${$name}}";

echo "This is the value of the var named by the return value of getName(): {${getName()}}";

echo "This is the value of the var named by the return value of \$object->getName(): {${$object->getName()}}";
?>

Bunu yapabilirim, çünkü "kaçış" demek:

$a = 'abcd';
$out = "$a $a"; // "abcd abcd";

veya

$out = "{$a} {$a}"; // same

yani bu durumda kaşlı gereksiz ama:

$out = "$aefgh";

will, depending on your errveya level, either not wveyak veya produce an errveya because there's no variable named $aefgh so you need to do:

$out = "${a}efgh"; // veya
$out = "{$a}efgh";

Bana gelince, kaşlı birleştirme için bir ikame olarak hizmet, onlar quicker yazın ve kod temiz görünüyor için. Tek tırnak ('') size literal name değişkeni sağlanan alırsınız, çünkü bunların içeriği, parsed PHP ile olduğu gibi çift tırnak ("") kullanmayı unutmayın:

<?php

 $a = '12345';

// This works:
 echo "qwe{$a}rty"; // qwe12345rty, using braces
 echo "qwe" . $a . "rty"; // qwe12345rty, concatenation used

// Does not work:
 echo 'qwe{$a}rty'; // qwe{$a}rty, single quotes are not parsed
 echo "qwe$arty"; // qwe, because $a became $arty, which is undefined

?>

Quickly...

Bu örnek bir Kitap için:

$number = 4;
print "You have the {$number}th edition book";
//output: "You have the 4th edition book";

Kıvırcık whitout parantez PHP adlı bir değişken $ numberth bulmaya çalışın, o yok!

Burada ben bir wordpress eklentisi var kodu

$data = $wpdb->get_results("select * from {$wpdb->prefix}download_monitor_files");

Bu gerçekten karmaşık dizeleri biçimlendirme için kullanışlı bir tekniktir.