My code works, but I'm having trouble with line breaks...
I have data saved to a variable as such within a foreach loop:
$printerdata .= " " . $product_name . "\n";
$printerdata .= " $". $price . ".00 \n";
$printerdata .= " " . $displayoptions . "\n";
$printerdata .= "\n";
Then I post that data and use sockets to print to the printer from my php code. Like I said before, my code works great, but the problem lies with line breaks. When printed from one browser the data is displayed correctly with "\n" from the code above, however, when printed from the browser I'm being forced to use called Fresco, I get whitespace between the data rather than line breaks.
I've tried "\r\n" and that still does not produce any line breaks. I've also tried <br> but is literally printed out. Does anyone have any ideas on other ways to produce line breaks within my code? Thanks for the help!
The php function nl2br could help you. This function inserts HTML line breaks before all newlines in a string. You can look it up here:
nl2br Manual
The correct tag is
</br> not <br>
But I think in fresco there is also a tag
<af>
Related
I have a php variable that is created like so:
$scholarshipTitleOutput = ($scholarship['field_sec1_title']['#items'][0]['value']);
If I print the variable by itself it renders correctly, but I want the output to be placed between two header tags. What I would expect to be able to do it echo out the following...
echo "<h2>" . $scholarshipTitleOutput . "</h2>";
But this removes the variable output from the page for some reason. I've tested further and found that I can concatenate after echoing the variable to do things like add a space...
echo $scholarshipTitleOutput . " ";
So what is it about that first example that is completely removing the variable from outputting on the page?
It works fine when I try it. You can also try
echo "<h2>{$scholarshipTitleOutput}</h2>";
I am trying to use \n in PHP to get a new line on my website, but it's not working.
Here is my code:
if(isset($_POST['selected'])){
$selected_val = $_POST['selected'];
// Storing Selected Value In Variable
echo "\n";
echo $selected_val; // Displaying Selected Value
}
On echo, use <br />.
The \n won't work in the HTML page, only in the source code, executing the PHP from the command line or writing into a text file.
This essentially boils down to saving the below text to a HTML file and expecting it to have visual line breaks:
one line
another line
To have a visual line break in HTML you're gonna need the br element:
one line<br />
another line
So replace your echo "\n"; with echo '<br />';.
If you have a string containing newlines, you could use the php built in nl2br to do the work for you:
<?php
$string = 'one line' . "\n" . 'another line';
echo nl2br($string);
When writing PHP code, you need to distinguish between two distinct concepts :
go to the new line in the code you produce, which you do using "\n"
go to the new line in the HTML webpage you produce, which you do using <br />
So, Option 1 makes you go to the new line in the code you produce, but you will not go to a new line in the HTML webpage you produce. The same way, option 2 makes you go to the new line in the HTML webpage you produce, but you will not go to a new line in the code you produce.
If you want go to the next line in both your code and the HTML output, you can just combine "\n" and <br /> :
echo "<br />\n";
On a web page, out of a <pre> block, all occurrences of tabs and newlines characters are assimilated as a single space.
For example:
echo "<pre>\n" . $selected_val . "</pre>";
But if your code is for debugging purposes, you'd better use print_r to inspect your variable values. Such as:
echo "<pre>" . htmlspecialchars(print_r($select_val), true) . "</pre>";
The htmlspecialchars function preserving you from ill-formed HTML due to the variable content.
I'm trying to get a URL to have a dynamic parameter based on the value of a variable.
Here's what Im doing
echo "The file name is ".$value.''.$value.''."<br>";
However, when I click on the corresponding link I get the following URL in the browser
http://localhost/HelloWorld/filespecificpage.php?filename=<?php echo $value ?> rather than the actual value in $value. Any help on how I can fix this would be appreciated.
PHP Noob here, appreciate the patience.
You're already in PHP script mode, you don't need <?php. You just need to concatenate the variable, like you did earlier in the line. You should also use urlencode() when substituting a variable into a URL parameter.
echo "The file name is ".$value.''.$value.''."<br>";
If you break the PHP String/Echo dont use the doublequotes, it makes your live much harder as you need ist.
For your question
echo 'The file name is ' . $value . '<a href="filespecificpage.php?filename=' . urlencode($value) . '>' . $value . '</a><br>';
An expample why single and not doublequotes. If you write an Hyperlink in HTML you use
Link description
and all is fine.
If you do it in an PHP echo with doublequotes you must escape all quotes.
echo "Link description";
Perfect look and way
echo '<a href="website" target="_blank" ' . $anyPHPvar . '>Link description</a>';
and you can use " for clear html and ' for PHP ;)
So, use singlequotes makes your life easyer and its a little bit faster and welcome to PHP ;)
Well it's a very simple question but I haven't found the answer. Hopefully it'll not take too much of anyone's time to just show me.
I want to output a multi line die() message and I've tried variations of \r\n and PHP_EOL with quotes here and not here and things like that: and I can't get it to work.
Here's the basic line I want to edit:
if(mysql_num_rows($res) > 0) die("You Will Be Reported For Spam To Your ISP" . mysql_error());
I just want to expand that "you will be reported..." into a little para of dire threats..
That's because whitespace is folded in HTML. If you need to break the line then you will need to do it as HTML does it: with the <br> tag.
Does this help?
<?php
// line breaks will appear if running on command line
if(mysql_num_rows($res) > 0) {
die("You Will Be Reported\r\nFor Spam To Your ISP" . mysql_error());
}
// paragraphs will appear on a web page
if(mysql_num_rows($res) > 0) {
die("<p>You Will Be Reported</p><p>For Spam To Your ISP</p><p>" . mysql_error() . "</p>");
}
?>
Mate, if you want to die() preformated text (having \r\n) not in html context just use
die("<pre>" . $your_multiline_preformated_text . "</pre>");
this should do it. If you want to use HTML tags in your die() output, you need to inform the browser first (so either printout some "< html >< body >" first or feed the browser with a php header 'content-type').
I have an issue with a PHP script I'm developing. Sometimes (very rarely, but it occurs) strings don't get concatenated correctly, as fragments of the string get lost during the process, resulting in randomly truncated strings.
This is an example, it's part of the code that gathers the order info and sends it via email to the client (that's why I'm forced to use html tables).
$rowList[$rowCode] .= '<tr><td class="lens-price-serv"><small>' . $lang['services'] . ':</small> <strong>' . money_format( '%i', $srvPriceTotal ) . '</strong></td></tr>';
The code works just fine. This morning, though, I got an email with the following html (corresponding to the PHP code I posted):
<tr><td class="lens-price-serv"><s>Services: <strong>€ 10,00</strong></s></td></tr>
I couldn't get the exact html code, this one comes from Chrome's Developer Tools. What I think happened is that the 'small' tag got truncated during string concatenation, becoming an open 's' tag. This tag, without the corresponding closure '/s' tag, got automatically closed by the browser just before closing the 'td'.
This issue is hard to replicate. I tried making a new order using the same parameters, but it worked just fine.
This is the second time this issue presented itself; the first time it was on another part of the code, but the string got truncated as well, resulting in wrong output.
Could this issue be caused by the server? This script is hosted by a shared hosting running Apache 2.2.24 and PHP 5.3.21.
Try to use
$rowList[$rowCode] .= "<tr><td class='lens-price-serv'><small>$lang['services']:</small> <strong>" . money_format( '%i', $srvPriceTotal ) . "</strong></td></tr>";
You can also try
$rowList[$rowCode] .= "<tr><td class='lens-price-serv'><small>{$lang['services']}:</small> <strong>" . money_format( '%i', $srvPriceTotal ) . "</strong></td></tr>";