I am trying to display different content on a page based on some options.
Also, I am trying to avoid using php echo for all the html output.
I came up with the following solution accidentally, and now I'm confused about how it actually works.
test.php
<?php
function get_content() {
$page = 0;
if($page == 0)
include('page0.php');
else
include('page1.php');
}
?>
<html>
<body>
<?php echo get_content() ?>
</body>
</html>
page0.php
<?php
$link = "http://www.google.ca";
$name = "GOOGLE";
?>
<?= $name ?>
page1.php
<?php
$link = "http://www.yahoo.ca";
$name = "YAHOO";
?>
<?= $name ?>
It seems like the php interpreter would end up including html tags into a <?php ?> block when it reaches the following line, but somehow, this code works, and the outputted html is valid.
include('page0.php');
Can someone explain what exactly is going on here?
When a file is included, parsing drops out of PHP mode and into HTML
mode at the beginning of the target file, and resumes again at the
end. For this reason, any code inside the target file which should be
executed as PHP code must be enclosed within valid PHP start and end
tags.
From PHP manual, include function.
Related
I currently have a php which echo my html template.
However in that HTML template there is another echo which calls from another php script.
Just wondering how do I do that? Because once I echo my html template the other it doesn't seems to echo my content from the other php script.
HTML TEMPLATE
<php? $html = '<span>name:<?php echo $name; ?></span><span>email:<?php echo $email; ?></span>' ?>
CONTACT TEMPLATE
<php? $name = "hello world"; $email = "hello#world.com"; ?>
I can see what you're trying to do, and it's a simple error. You can't escape php like that whilst inside setting a variable.
Also, I must add that you are declaring php incorrectly.
This is preferred
<?php
not
<php?
So make sure for your contact template you use the correct tag.
Also to include a file you have to call it/require it.
Back to the original question - Here is your method
<php? $html = '<span>name:<?php echo $name; ?></span><span>email:<?php echo $email; ?></span>' ?>
Here is the correct method
<?php
require('contact.php');
$html = '<span>name:'.$name.'</span><span>email:'.$email.'</span>';
echo $html;
?>
First I created the variable. And when doing so I insert the existing variables by escaping the php. Only once this final variable is created do I echo it.
Hope this helps you on your way.
Try to use include. The include statement includes and evaluates the specified file, in this case - your template.
Just Concatenation
<?
$html = '<span>name:'.$name.'</span><span>email:'.$email.'</span>';
?>
Change the tags from <php? ?> to <?php ?> in your script
I know it has been discussed many times, but I can not find any solution to this problem.
I have a PHP file with the following code
if (file_exists("email.html")) {
$message = file_get_contents("email.html");
} else {
echo "No email.html file present";
return;
}
in my file email.html I have something like:
<p>Hello %recipient.UserName%, you receive this Email because you signed up at our site.</p>
variable $UserName is declared in the php file (in array).
When looking at the html file output, I see the variable is not correctly passed and stays as % %
Any suggestion?
thanks!
If you don't have a template engine you can't do it with % % OR other symbol . for your question simple approach is to use it like this :
<p>Hello <? = $recipient['UserName'] ?>, you receive this Email because you signed up at our site.</p>
EDIT
you have to include the file
ob_start();
include "email.html";
$message = ob_get_clean();
The below code will not display any output as the variable is declared below the echo as PHP gets executed line by line. Is there any way to search for the variable in the whole page and then execute the code?
<?php
include "header.php";
$title = "Test";
?>
header.php
<html>
<head>
<title><? echo $title ?></title>
</head>
You need to learn how compilers/interpreters works. PHP is interpreted language and The binary that lets you interpret PHP is compiled.
PHP run from top to bottom.
so its like
<?php // start from here
echo "$title"; <-- $title is undefined here
$title = "Test"; <-- now you declared $title with value so it goes in memory now
//end
So you need to first check weather $title is set or not than respond according to it
if(isset($title)){
echo $title;
}
According to your logic, I suggest you to use contants like below:
Create a separate file, let's say constant.php and include it on all other pages
<?
define("TITLE", "This is title");
?>
Use it like below:
<?php echo TITLE;?>
Thanks
<?php
function outLala() {
?>
lala
<?php
}
?>
the result of this php file is a blank page instead of a page with text "lala".
if i change the code as
<?php
function outLala() {}
?>
lala
<?php
{}
?>
the result of this php file is a page with text "lala".
does it mean that the php interpreter will parse the code inside the open and the close tag, and if it finds a function(or something else) is not properly terminated, the interpreter will think the content following the close tag is part of the function?
This is valid PHP, but you don't call the function. Add
outLala();
at the bottom.
You must call function outLala. Example:
<?php
function outLala() {
?>
lala
<?php
}
outLala();
?>
I wrote a php page which has two php tags and one script tag inside it .
<?php
$value = $_GET['hash'];
?>
<script>
function execute(){
<?php
$readfile = file($value);
for ($k=0;$k<=count($readfile)-1;$k++){
$cmd = $readfile[$k];
echo $cmd;}
?>
}
</script>
I want to use $value inside another php tag ( like above it has the file I want to open ), but I am not able to do it.Is the scope of variable limited to one php tag ? if yes how can I solve this problem Please help
Your code works perfectly. The variables in one PHP tag is accessible from all other tags, unless you define them inside a PHP function.
The reason you are not seeing the echo on the screen is because the echo prints to the Javascript function.
If you view the source of the generated page, the file contents will be there.
Try this:
function execute(){
<?php
$readfile = file($value);
for ($k=0;$k<=count($readfile)-1;$k++){
$cmd = $readfile[$k];
?>
alert( <?php echo $cmd; ?> );
<?php
}
?>
}
execute();
if $value is a get then you don't need to access it as a file, it should just be a short string.
just above line 7 (the one with $readfile = file...
type:
echo "alert(The hash value is: ".$value.")";
This will make an alert display (as it is in a script tag)
p.s you should have in your opening tag