Reputation: 1818
I am learning php and am making a statement that proves the local variable cannot be called outside a function where it is defined.
I can't seem to get a line break come out in the statement but only a line space.
<html>
<head>
<title>Testing Global and Local Variables</title>
</head>
<body>
<?php
//Global varaible called after function
$g_test_string="this is the value in the variable outside the function at a global level!";
function printstring()
{
$l_test_string="this is the value of the variable inside the function at a local level!";
print($l_test_string);
}
//Calling statements
printstring();
echo "\r";
echo 'Global variable value: ' . $g_test_string;
echo 'Local variable value: ' . $l_test_string;
?>
</body>
</html>
The print out looks like this:
this is the value of the variable inside the function at a local level! Global variable value: this is the value in the variable outside the function at a global level! Local variable value:
I tried a break between the function call and echo for global variable but only produces a line space not a break.
Got to be something simple I imagine.
Many thanks
Andrew
Upvotes: 2
Views: 9839
Reputation: 122
A workaround if you are trying to print "\n"
on your localhost, try adding following in your script on the top.
header('Content-type: text/plain');
Not an ideal solution but works.
Upvotes: 0
Reputation: 1
How break the line on display data one by one line on while loop and forward to dropdown id
$que="select * from parties WHERE Item_code='".$_GET['q']."'";
$run=mysql_query($que);
while($row=mysql_fetch_array($run)){
if($row['party_name'] == "")
{
echo "";
}
else
{
if($_GET['type'] == "di")
{
echo $row['Id'];
}
elseif($_GET['type'] == "did")
{
echo $row['party_name'];
}//END IF DI OR DID
}//END IF PARTY NAME BLANK
}//END WHILE
Upvotes: 0
Reputation: 3729
If you want to be sure your line break is cross platform compatible, use PHP_EOL
I usually use "\n". If you want the linebreak to appear in HTML, use "<br>"
.
Edit in response to comments: Here's how you pull as much PHP out of the HTML as possible.
<?php
//Global varaible called after function
$g_test_string="this is the value in the variable outside the function at a global level!";
?>
<html>
<head>
<title>Testing Global and Local Variables</title>
</head>
<body>
<?php
//Calling statements
printstring();
echo "<br>\n";
echo 'Global variable value: ' . $g_test_string."<br>\n";
echo 'Local variable value: ' . $l_test_string."<br>\n";
?>
</body>
</html>
<?php
function printstring()
{
$l_test_string="this is the value of the variable inside the function at a local level!";
print($l_test_string);
}
Upvotes: 2
Reputation:
Where you want a line break in PHP use either <br />
or \n
I used breaklines for your demo: http://www.forumalliance.net/so.php
<html>
<head>
<title>Testing Global and Local Variables</title>
</head>
<body>
<?php
//Global varaible called after function
$g_test_string="this is the value in the variable outside the function at a global level!<br />";
function printstring()
{
$l_test_string="this is the value of the variable inside the function at a local level!<br />";
print($l_test_string);
}
//Calling statements
printstring();
echo "\r";
echo '<br />Global variable value: ' . $g_test_string;
echo '<br />Local variable value: ' . $l_test_string;
?>
</body>
</html>
Upvotes: 1