Use PHP variable at different moments in code [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 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.

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 HTML {$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 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

how to create a line break in php using "\n"? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I am very new in PHP programming and my line break does not working.
Please find the bug, in the below code:
<?php
echo "hello World \n ";
//the next section will demonstrate the use of variable
$name='Nadim Emon';
echo "Hello $name";
?>
Here is the working code, direct use this code it will work
<?php
//the next section will demonstrate the use of variable
$name='Nadim Emon';
echo nl2br("hello World.\nHello $name.");
?>
The line break works fine, but the browser displays HTML entities, not line breaks.
Change your code to this:
<?php
echo "hello World <br> ";
//the next section will demonstrate the use of variable
$name='Nadim Emon';
echo "Hello $name";
?>
Browsers display code that has specific values and generally ignores whitespace. You can get around this by using some very specific HTML tags such as <pre> which will then maintain line breaks in your output.
There is also another option such as PHP's nl2br which will then replace any line breaks with HTML code to place line breaks in as you expect them to be displayed by a browser.
you can use the <br/> tag
<?php
echo "hello World"."<br/>";
//the next section will demonstrate the use of variable
$name='Nadim Emon';
echo "Hello $name";
?>

Using PHP Wrapper Tags (<?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 8 years ago.
Improve this question
Coming from C++/C/C# the use of the tags keeps nagging at me. When writing php scripts inside a php file, do the tags have to be around EVERYTHING, or is there any special syntax for outside of these tags? Also, can there be multiple sets of tags within the same file (even if it's against convention).
PHP is designed to be embedded into web pages. So the way it works is that it simply copies its input file to the output, until it encounters the <?php tag. Anything inside this is executed as PHP code. When it sees ?>, it goes back to copying mode. So all executable code has to be inside <?php ?>, and verbatim text can be outside it.
Coming from C, just think of everything outside the tags as a big printf() statement.
Yes, tags should be around every block of PHP code. There is multiple ways of doing this, one way would be doing an entire file in PHP, eg.
<?php
$var = "my text";
echo '<html>';
echo '<head>';
echo '</head>';
echo '<body>';
echo '<p>' . $var . '</p>';
echo '</body>';
echo '</html>';
?>
Another way would be to do it all inline
<html>
<head>
</head>
<body>
<p><?php $var = "my text"; echo $var; ?></p>
</body>
</html>
As far as I'm aware, there's not really a preferred convention, many people do one or the other, or both, depending on how much php is required. This information is what you would really find from ANY php tutorial site, and is not really suitable for Stack Overflow.
But regarding your question of whether all php needs to be in the <?php ?> tags, the answer is yes, that's how the server knows where to compute code before sending the file off to a request.
edit:
php will also keep track of values after the tag has closed. One block of tags does not work the same as curly brackets in C oriented programs, so in the following case:
<?php
$var = "text"
?>
...
<?php
echo $var;
?>
the output would still be text.
This also applies to functions in php, so if you have
<?php
function myFunction(){
$var = "text";
?>
<li>
<?php
echo $var;
}
?>
It won't break out of the function because you closed the tag.

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