Is there any difference between <?=$value?> and <? echo $value; ?> - php

Environment: Windows/7 + Apache/2.2.21 + PHP/5.3.8
File contents of test.php:
hello, <?=$test?>
File contents of index1.php:
<?php
$test = 'world';
require './test.php';
?>
File contents of index2.php:
<?php
global $test;
$test = 'world';
require './test.php';
?>
Output of index1.php is:
hello,
Output of index2.php is:
hello, world
When the contents of test.php is:
hello, <? echo $test; ?>
Output of index1.php and index2.php both are:
hello, world
So, my question is: Is there any difference between <?=$test?> and <? echo $test; ?> ?

No, there is no difference. Only one I think about is that <? is considered as short tag and might not work.

There is a little difference, that can be very, very annoying. If in php.ini you short_open_tag is set to false, you will receive a lot of errors. Otherwise, is exactly the same.
In every case, the last ; before ?> is optional.

There is no output differnce between <?=$x;?> and <? echo $x; ?> for that matter.
Even though i think this technique of including an active PHP file isn't really best practices in this specific condition.
Shai.

Related

Forcing file_get_contents() not to render php codes as plain text

How can I get expected output from example below?
Note: I'm using $content = file_get_contents('content.php'); to use content where and when possible so it is not a direct output on screen. include() breaks the pages.
content.php
<p>Hello <?php echo 'World!'; ?></p>
reader.php
<b>Message from another file:</b> <?php echo file_get_contents('content.php'); ?>
Output of code above is:
Message from another file: Hello <?php echo 'World!'; ?>
Instead of (expected):
Message from another file: Hello World!
I think you are looking for <?php include('content.php');
file_get_contents — Reads entire file into a string
PHP.net file_get_contents - manual
The include statement includes and evaluates the specified file.
PHP.net include - manual
Try making content.php into a file that has a function that returns the content you want (you may want to have parameters). Simply require the file then call the function and save the output.
Example:
content.php
function get_content($world){
return '<p>Hello ' . $world . '</p>';
}
reader.php
<?php
require('content.php');
$content = get_content('world');
?>
<b>Message from another file:</b> <?php echo $content; ?>
Since you cannot use include (though I don't understand fully why), but want the file to be parsed and executed as PHP code, you can use eval
<b>Message from another file:</b> <?php eval(file_get_contents('content.php')); ?>
But the file content.php should not contain <?php and ?> tags, as stated at http://php.net/eval.

$string is not showing in common.php

Let's say I've got 2 files. 1 is common which loads all the design and stuff and one is index.
What I want to do is set a $ in index like this:
<?
$SubId3 = 'test';
include "../../common.php";
?>
Then in common I want to have something like
<?=$SubId3; if (empty($SubId3)) { echo 'homepage'; } ?>
I cannot seem to get this working. Meaning if I set it up this way. The index will never show "test".
What am i doing wrong here?
I want to do this since only certain files will contain the string $SubId3, to test some things on certain pages and not others (by adding $SubId3 = 'test'; to that particular file)
Note that <?= is short-hand to output something (think of <?= as <?php echo) and not to execute any other sort of logic or code.
However, it is possible to use the ternary operator this way:
<?= empty($SubId3) ? 'homepage' : $SubId3; ?>
This is basically equivalent to this:
<?php
if (empty($SubId3)) {
echo 'homepage';
}
else {
echo $SubId3;
}
?>
So the <?= short-hand should only be used to pass one simple variable or a ternary expression to it; everything else should use the common <?php tag.
Here's a test case for Alex (in the comments) because I can run the above code just fine with PHP 5.4.12, but he seems not to be able to.
common.php
<?= empty($SubId3) ? 'homepage' : $SubId3; ?>
index.php (visit this file then)
<?php
$SubId3 = 'test'; // <-- Comment this out for the "homepage" output
include 'common.php';
i think this
<?=$SubId3; if (empty($SubId3)) { echo 'homepage'; } ?>
should be
<?php $SubId3; if (empty($SubId3)) { echo 'homepage'; } ?>
<?=?> is short for <?php echo?>
This wont work:
<?=$SubId3; if (empty($SubId3)) { echo 'homepage'; } ?>
If you want to print some stuff, you have to use only the variable, in one block and the IF on another.
<?=$SubId3?>
And:
<?php if(empty($SubId3)) { echo 'homepage'; } ?>
Hope this helps...
Try
<?php
/* echo $SubId3; */
if (empty($SubId3)) {
echo 'homepage';
} else {
echo $SubId3;
}
?>
Consider using different style of coding.
In PHP you have generally three variants:
PHP code only
HTML files with just some echoes
Intermixed PHP and HTML
In first you use echo to output every single bit of the HTML.
Second means you include a PHP script at the top of your HTML file and call appropriate functions / insert text into the template. Just so you can edit your HTML separately from your PHP.
Third makes for sometimes unreadable and complex code, but is fast to write.
<?php if($something) {
while($otherthing) { ?>
<B>text=<?=$index ?></B>
<?php }} ?>
Just a food for thought.
I found the answer guys, thanks for all the help.
I needed to set it in the PrintHeader like this:
<?
include "../../common.php";
printHeader('BlogNr1', 'BlogNr2', 'BlogNr3');
?>
And the index had to look like this:
<?
include "../../common.php";
printHeader('BlogNr1', 'BlogNr2', 'BlogNr3');
?>
Somebody on skype helped me. thanks anyways guys!

Require found file, doesn't show String

First I'm sorry if this question is duplicated but I tried to find here the solution and spent some time reading some tutorials but I haven't found the solution.
I'm starting to learn PHP. I use Easy PHP 13.1 VC9.
In my testing project I have to files:
index.php:
<?php
global $str_texto;
echo 'before ';
require ('vars.php');
echo "$str_texto";
echo ' after';
?>
vars.php:
<? php
global $str_texto = 'String text';
$data_hoje = date('j / F / Y');
?>
When I start index.php my browser shows the page without errors (so I assume that EasyPHP found the file). It shows the words "before" and "after" but doesn't show the $str_texto string.
I compared my code to some code available on web like this available on PHP: Include - Manual :
vars.php
<?php
$color = 'green';
$fruit = 'apple';
?>
test.php
<?php
echo "A $color $fruit"; // A
include 'vars.php';
echo "A $color $fruit"; // A green apple
?>
The same problem happens with that sample so, on my code, I changed the Include command to Require so I can get an error message. Same behavior and no error. There's a blank space between "before" and "after" were should be "String text". Declaring $str_texto as global and change quotation marks to single quotes didn't solve the problem.
I wasn't able to find any problem on web like this. Can you help?
There is a space in your php declaration in vars.php
<? php
Should be
<?php
Try this without quotes because is a variable:
echo $str_texto;
AND
change open-tag:
<? php
to
<?php
Try this:
var.php:
<?php
global $str_texto;
$str_texto = 'String text';
$data_hoje = date('j / F / Y');
?>
index.php:
<?php
global $str_texto;
echo 'before ';
require ('vars.php');
echo $str_texto;
echo ' after';
?>

Using php variables in included html

I'm looking for something much like the Using PHP variables inside HTML tags? question, but a little different.
In my case, I'd like to use code ore like this:
$somevar = 'a test';
include("file.html");
and file.html would contain
<b>hello, this is {$somevar}</b>
The problem is that it just prints hello, this is {$somevar}.
How can I make the HTML read the vars in the included file?
echo "<b>hello, this is {$somevar}</b>";
or
<b>hello, this is <?=$somevar?></b>
or
<b>hello, this is <?php echo $somevar; ?></b>
or
<b>hello, this is <?php print $somevar; ?></b>
You need to include the variable defining program, in the other program wanting to access it.
Example:
Say test.html has $somevar.
in file.html you do,
<?php
include('test.html');
echo "<b>hello, this is $somevar</b>";
?>
<?php
include "stuff.php";
$somevar = "test";
?>
<html>
<body><p><?php echo($somevar); ?></p></body>
</html>

PHP eval() code in between <?php ?> from database

I want to be able to put PHP into the database and run it. I have to do this because I store page layouts in the database and each our different for each other, however in some cases I want to use dynamic content for some of the pages.
Assume $query_from_db is the string returned from the database. PHP should only eval() the code in between <?php and ?>
$query_from_db = '<div>
<?php
//php to run
function dosomething() {
//bleh
}
?>
</div>
';
php echo eval($query_from_db);
How can I do this? I'm aware this is not recommended.
I'm not arguing about the sense or nonsense of this approach. To some extend, this is a valid question.
See the documentation:
To mix HTML output and PHP code you can use a closing PHP tag to leave PHP mode.
So you have to do:
eval('?> ' . $query_from_db . ' <?php ');
DEMO
Also note that eval is outputting directly to the browser. It does not return a value. Have a look at Output Control Functions for buffering.
You are aware that this is not recommended and I strongly urge everyone to review the comments to this question.
But to provide an answer:
<?php
$string = 'hello <?php echo "world"; ?>';
eval('?>'.$string.'<?'); // will output "hello world";
be aware that this however will not work:
<?php
$string = 'hello <?php echo "world"; ?>';
eval('?>'.$string.'<?php'); // error will be thown
This works again:
<?php
$string = 'hello <?php echo "world"; ?>';
eval('?> '.$string.' <?php '); // will output "hello world";
i am not really sure why.
following up on your comment to grab the output you can do:
<?php
$string = 'hello <?php echo "world"; ?>';
ob_start();
eval('?> '.$string.' <?php '); // will output "hello world";
$output = ob_get_clean(); // $output will now contain "hello world". No text will have ben printed.
If you want to avoid the eval stigmata, you can alternatively use:
include("data:,$query_from_db");
It's just another name for eval which doesn't upset people as much. It depends on the php.ini setting allow_url_include however.
What you are doing is functionally equivalent to include("$template/$by_name.php"); and just differs in that you didn't put the database content into a file before. (But that's the other workaround: file_put_contents && include).

Categories