PHP HTML {$variable} [closed] - php

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
How can i print variable in brackets? Like ip board, for example I have this code:
<?php
$variable = "test";
?>
<div class="test">{$variable}</div>
How can I do this? Not
echo $variable;?>
but {$variable} ! Thanks in advance :3

It's called concatenation and it uses '.' to string things together...
<div class="test"><?php echo '{'.$variable.'}' ?></div>

you can use it like this
<?= $variable ?>

Use a php templating engine. Best for you is that use Laravel and Balde

yes, you can just do it like
<?php
$variable = "test";
?>
<div class="test"><?= $variable; ?></div>
but if you want to use above approach(your one) you've to use blade templating engine
have a look

Related

How to get the source code of current page as a variable in php [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
for example my page is like below code. I know how to extract "this" from the string by preg_match but how do i get the source code of current page as a variable in php.
<body>
<p>I need to find "this" word from "this" string</p>
</body>
Call ob_start() at the beginning of your script, then ob_get_clean(), at the bottom. It will return the output contents of the script as a String, which can you store in a variable. So, from your example:
<?php ob_start(); ?>
<body>
<p>I need to find "this" word from "this" string</p>
</body>
<?php $foo = ob_get_clean(); ?>

PHP is not closing [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I put in the tag and inside it is this:
$one = "\Images\";
I correctly closed the tag itself, but everything after it is being considered part of the tag.
Full code:
<div id="skyscraper-ad">
<?php
$one = "\Images\";
?>
</div>
In PHP the \ character is used to escape an immediately following character that could be interpreted as 'not part of the string'.
eg. If you were to run this:
echo '\'hello';
it would output: 'hello.
In your code, you're escaping the ending ' which will make PHP throw an error.
echo '\Images\\'; on the other hand will output \Images\
You need to escape the backslashes:
<div id="skyscraper-ad">
<?php
$one = "\\Images\\";
?>
</div>

I have trouble with php variable [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have a little question about variables in php.
How can I make this possible??
<?php
echo $test;
$test = 'This is a test';
?>
I know that you could fix it with ease with
<?php
$test = 'This is a test';
echo $test;
?>
but I cant use it in my page in that way.
Can any one please tell me how I can have a variable after the echo??
Buddy.. It can't be Done.. How you can output a value of a variable when its not assign to it..
Its common sense..
<?php
echo getTest();
function getTest(){
return 'This is a test';
}
You can't. A variable has to be declared before it has a value.

Use PHP variable at different moments in code [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
Is it possible to have use a php variable in multiple positions in the code? For example
<?php
some code here with a certain variable called var
?>
some html code
<h3> <?php $echo $var ?> <h3>
Yes! PHP is wonderful that way. I like to think of it as an overlay on top of my HTML code that simply acts when I tell it to. You can test it for yourself and see - that's the best way to learn :)
This works fine:
<?php
$a = "hello world!";
?>
<h3> <?php echo $a ?> <h3>
If a file is being interpreted as a PHP script you may think of it as a entire piece of code, the HTML code between scripts works like a echo. So, if you declare a variable in any part of the script, if it is on the global scope, it is available for each of the scripts declared. Just be careful with the scopes where you declare de variables. If you declare a variable inside a function, it will be available inside the function.

How can I display php file to the screen [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
How can I display php file to the screen from this php file?
So php script should display own code.
Conditions - file and input statements are forbidden!!!
<?php
echo file_get_contents('you_php_file.php');
?>
or simply
<?php
echo file_get_contents(__FILE__);
?>
Read more about file_get_contents here and about __FILE__ here
use highlight_file so that you can get the source code in nice color formatting.
highlight_file($file_name);
If the PHP is loading, you could simply echo the code, though you have to escape the (double) quotes ofcourse:
Echo '$testvar1 = \'butterfly\';
$testvar2 = \'elephant\';';
Or if indeed you want to display all the code, donCallisto's method is better ;-)
To have a php file display its own code you could do it like so:
<?php
echo file_get_contents(__FILE__);
?>
to get another file's contents
<?php
echo file_get_contents("other_file.php");
?>
Use:
echo htmlspecialchars(file_get_contents($file));
(otherwise, if you use echo file_get_contents($file);
you have to click View>Source on page, to see the content, as they are put in <>)

Categories