PHP kıvırcık dizesi sözdizimi soru

3 Cevap

PHP 5.3.0 koşuyorum. Ben ifadenin ilk karakteri $ olduğunda kıvırcık dizesi sözdizimi sadece çalıştığını tespit ettik. Ifadelerin diğer türleri (fonksiyon çağrıları, vb) dahil etmek için bir yolu var mı?

Önemsiz bir örnek:

<?php
$x = '05';
echo "{$x}"; // works as expected
echo "{intval($x)}"; // hoped for "5", got "{intval(05)}"

3 Cevap

No çeşitli formlarının sadece değişkenler değişken ikame ile ikame edilebilir.

Bu linke bir göz atın LINK

Kod örneği,

Similarly, you can also have an array index or an object property parsed. With array indices, the closing square bracket (]) marks the end of the index. For object properties the same rules apply as to simple variables, though with object properties there doesn't exist a trick like the one with variables.

<?php
// These examples are specific to using arrays inside of strings.
// When outside of a string, always quote your array string keys 
// and do not use {braces} when outside of strings either.

// Let's show all errors
error_reporting(E_ALL);

$fruits = array('strawberry' => 'red', 'banana' => 'yellow');

// Works but note that this works differently outside string-quotes
echo "A banana is $fruits[banana].";

// Works
echo "A banana is {$fruits['banana']}.";

// Works but PHP looks for a constant named banana first
// as described below.
echo "A banana is {$fruits[banana]}.";

// Won't work, use braces.  This results in a parse error.
echo "A banana is $fruits['banana'].";

// Works
echo "A banana is " . $fruits['banana'] . ".";

// Works
echo "This square is $square->width meters broad.";

// Won't work. For a solution, see the complex syntax.
echo "This square is $square->width00 centimeters broad.";
?>

orada kaşlı ayraç ile elde edebilirsiniz farklı şeylerdir, ama bunu nasıl kullandığınıza bağlı olarak, sınırlıdır.

Eğer normal ayrıştırma aksi olmayabilir kuralları bir değişken olarak bir şey tedavi PHP zorlamak gerekir sürece genellikle, değişkenler etrafında parantez gerekmez. Büyük bir boyutlu diziler olduğunu. PHP'nin ayrıştırıcı bir değişken ne karar vermek için olmayan açgözlü ve ne değildir, bu yüzden parantez dizi öğesi başvuruları geri kalanını görmek için PHP zorlamak için gerekli olan:

<?php

$arr = array(
    'a' => array(
         'b' => 'c'
    ), 
);

print("$arr[a][b]"); // outputs:  Array[b]
print("{$arr[a][b]}"); // outputs: (nothing), there's no constants 'a' or 'b' defined
print("{$arr['a']['b']}"); // ouputs: c