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";
?>
Related
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>
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.
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.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 9 years ago.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Improve this question
Hello I'm new to html and php. Just programming in general. Anyways, I'm pretty sure this should be a working code for making a random number between 1 to 6:
<?php
echo (rand(1,6))
?>
Problem is when I put it inside my php document and test it in browser it doesn't show anything really.
That code does work.
I'm guessing that the you do not understand how php works. You need to have a web-server/localhost set up in order to properly run php files. I suggest you use this if you are a windows guy http://www.wampserver.com/en/
Even though your code will work on its own, it is strongly suggested that you close it with a semi-colon because you are including it from another file.
Ending semi-colons are recommended most or all of the time.
A semi-colon is not required for the last line to be echo'ed.
Consider the following:
(valid)
<?php
echo "Line 1";
echo "Line 2";
(invalid)
<?php
echo "Line 1"
echo "Line 2"
(valid)
<?php
echo "Line 1";
echo "Line 2"
Problem is when I put it inside my php document and test it in browser it doesn't show anything really.
Include example:
<?php
include 'include_rand.php';
echo "Line under random file";
?>
(include_rand.php file)
<?php
echo (rand(1,6)) // invalid because of content below the include
?>
In order for it to work, you would need to add a semi-colon at the end.
<?php
echo (rand(1,6)); // valid for include
?>
In a nutshell
The omission of a semi-colon on the last line of code is kind of saying that, it's awaiting further instructions. But if something is to be strung along under it, then it will produce an error and will not proceed any further.
To run a php script, first of all you have to install a php server(WAMP/XAMPP/) and put your php file inside the directory, WWW for WAMP and htdocs for XAMPP. And most importantly check your php syntax.
<?php
echo rand(1,6);
?>
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 <>)