Eğer yoluyla dahil bir dosyaya bir değişken geçmek için bir yol olup olmadığını merak ediyorum (dahil)?
Ben bu denedim ama bir hata var:
include("header_alt.php?img=hey");
Bunu yapmak için bir yolu var mı?
Teşekkürler!
Sadece ilk dosyasında değişken tanımlamak; Örneğin, in temp.php
,
<?php
$my_var = 10;
include 'temp-2.php';
die;
?>
Ve ikinci dosyada kullanabilirsiniz; temp-2.php
:
<?php
var_dump($my_var);
?>
Ve bu çalışması gerekir: Ben gelen, bu çıktıyı alıyorum temp-2.php
:
int 10
The query-string syntax, using stuff like ?img=hey
is used when you are requesting some data from a distant server (like when you are using your browser to surf on websites), not when including a file that is on the same server.
Smarty Bu tür bir şey için gerçekten iyi bir mekanizma sağlar. Artı, Smarty kullanarak sadece daha iyi php uygulamaları için yapar.
http://www.smarty.net/manual/en/language.function.include.php
Variables can be passed to included templates as attributes. Any variables explicitly passed to an included template are only available within the scope of the included file. Attribute variables override current template variables, in the case when they are named the same.
All assigned variable values are restored after the scope of the included template is left. This means you can use all variables from the including template inside the included template. But changes to variables inside the included template are not visible inside the including template after the {include} statement.