Unwanted space when Echoing a value - php

I want a comment to appear under a cell, whenever the $comment value exists.
Therefore I came with the following code:
$comment = "Enabled"; // add comment, whenever this value is not set to "0"
<td>
<form action="go.php" method="POST" target="_blank">
<input type="submit" class="a" value="click"></form>
<?= !$comment ? "" : "<div class=\"design\">Comment: " . $comment;"</div>" ?>
</td>
But in the output, I get unwanted space after Enabled which looks like:
<div class="design">Comment: Enabled </div>
What causes this?

$comment = "Enabled"; // add comment, whenever this value is not set to "0"
<td>
^^^^^^^^^^^^ 12
See here:
Enabled </div>
^^^^^^^^^^^^ 12
It's caused by the 12 spaces up there (I counted them). Plus, since you did not show your actual code as to where you closed your ?> tag, that will also contribute to it.
Therefore and as an example:
<?php
$comment = "Enabled"; // add comment, whenever this value is not set to "0"
?><td>
However, the following is off. The semi-colon's in the wrong spot and is missing a dot to concatenate:
<?= !$comment ? "" : "<div class=\"design\">Comment: " . $comment . "</div>"; ?>
So, avoid any spaces after closing ?> tags or before <?php. PHP's adding spaces for it and look at your HTML source; it's just as good a developer tool as any.

remove semicolon ";" and replace with dot "." in
". $comment;"</div>" ?>

Related

Checking for an empty string

I am looking for some guidance on checking for a string that is returning string(1) " ".
I am rendering a button in a WordPress template file. I first get the post meta, which is added via an API update. I then parse it so as to add the ID to an external booking url.
I need to test to ensure a value is returned of course. However, if I use !empty , this isn't working because even if the field has no value entered, it is still returning string(1) " " from the database.
I could test for strlen, but this doesn't seem very elegant. What would be the best way to test for this?
Many thanks
$s_apply_url0 = get_post_meta( $event_id, '_MyBookingURL', true);
parse_str($s_apply_url0, $output);
$s_apply_url1 = 'https://mybookings.com/event/' . $output ['id'];
if (!empty($s_apply_url0): ?>
<aside id="applybutton-2" class=" grop-side-widget widget_applybutton">
<a href="<?= $s_apply_url1; ?>" class="btn course-apply">
<?= $s_apply_text1 ?>
</a>
</aside>
<?php endif; ?> ```
If I've understood this question correctly, you want to be able to test whether the string is empty or contains more than one space characters
You could first use the trim function of PHP in order to 'trim' the string of whitespace (or, arbitrarily any other characters you wish to determine as 'blank').
<?php
if (empty(trim($myString)) {
// the string is empty, or contains only whitespace
}
If you wish to ONLY test for a single ' ' character, then you could just add that into your conditions, eg:
<?php
if (empty($myString) || $myString === ' ') {
// $myString is empty or only contains a single space character
}

how to change font color and adding space on function in php

please don't laugh at me, but i don't know how to change colors and add spaces between words inside php funtion.
here's my code:
<td><font color="green">
<?php
$response = json_decode($response, true); //because of true, it's in an array
echo $response['card']. $response['type']. $response['level']. $response['bank']. $response['country'];
?></font></td>
if you see that, i only know how to set the color to green only. what i want is that i can distinguish colors in each word and add spaces between words.
i tried to separate the php function one by one but that will cause an error. thank you.
You can use inline css styles, but it is not the best practice. Insert a string containing a space between your strings.
<?php $response = ... ?>
<td>
<span style="color: green;"><?php echo $reponse['card']; ?></span>
<span style="color: blue;"><?php echo $reponse['type']; ?></span>
</td>
In PHP you can concatenate string with:
echo $response['card'].' '.$response['type'].' '.$response['level'].' '.$response['bank'].' '.$response['country'];
To change the color, you should CSS instead of font tag that is deprecated. Something like:
echo '<span style="color:green">'.$response['card'].'</span> '.
'<span style="color:red">'.$response['type'].'</span>';
With this line:
$response = json_decode($response, true);
You create an array, which can be used further in the same page (read more about variable scopes).
So, after, you can use:
<td>
<font color="green"><?php echo $response['card'];?></font>
<font color="red"><?php echo $response['type'];?></font>
...
</td>
If you need to add spaces, you can do this with HTML or PHP.
With HTML just add a blank space " ". If you need more than one you can use special chars like
With PHP, you can concatenate strings using ., like in
echo $response['card'].' '.$response['type'];
To make spaces use .' '. in your case $response['card'].' '.$response['type'].
To change color use style style="color:red;" in your case echo "<span style='color:red;'>$response['card']</span>".

Get the .html after the echo

How to get the link of .html after the .$obj->product_name. ?
I have tried it as bellow. But it only shows the link of http://.com/forbiden insted of http://.com/forbiden rice.html where it cuts the rice.html
echo '<span class="product-name"><a href=products/'.$obj->product_name ."html". 'target=_blank>'.$obj->product_name.'</a></span></br>';
Try with urlencode($obj->product_name)
Hope that helps :)
Attribute values containing spaces must be quoted in HTML (and it is good practise to always quote your attribute values).
The space between forbiden and rice is terminating the attribute value.
URLs aren't allowed to have spaces in them anyway, so you should run the string through urlencode too.
You'll find it easier to deal with the quotes if you break out of PHP mode to output HTML instead of trying to mash it together in strings.
Also note that you should escape text content for HTML with htmlspecialchars as a defence against XSS.
$url = "products/" .urlencode($obj->product_name) . ".html";
?>
<span class="product-name">
<a
href="<?php echo htmlspecialchars($url); ?>"
target="_blank">
<?php echo htmlspecialchars($obj->product_name); ?>
</a>
</span>
<br>

Format PHP value with a CSS style sheet

I'm trying to format a PHP value from my script with a CSS style so it look the same then my html text preceding it.
Here is my code:
<p>Your surname is: </p><?php echo "<div id='php'>" $surname; "</div>" ?>
what i'd like to get is
Your name is Remi
all formatted the same way, what I get now is just an error and my PHP page does not show at all.
You have to use . (a dot) to concatenate your strings:
<p>Your surname is: <?php echo "<span id='php'>" . $surname . "</span>"; ?></p>
edit: you should use a span tag, not a div, to enhance your php output.
Your error is that you have a space between your string literal and your variable.
There is no need to echo the div tags from PHP, so don't.
You also shouldn't echo out raw text into the page, convert it to HTML first.
<p>Your surname is: </p>
<div id='php'><?php echo htmlspecialchars($surname); ?></div>
<p>Your surname is: <span id="php"><?php echo $surname; ?></span></p>
You could also go for the shorter notation:
<p>Your surname is: </p>
<div id='php'><?= $surname ?></div>
The semi-colon ends the statement. So PHP thinks that "</div>" after the semicolon is another PHP command. That causes an error and prevents the page from appearing. Move the semicolon so it's just before the ?>, and it should work.
Someone correct me if I'm wrong: Using a might cause some layout issues. I don't believe that "Your surname is" and the $surname value will appear on the same line.

How should I echo a PHP string variable that contains special characters?

I'm trying to populate a form with some data that contains special characters (e.g. single quote, double quote,<,>,?,","".~,,!##$%^&*()_+}{":?<<>,./;'[.] etc) :
<input type="text" name="message" size="200" maxlength="200"
value =<?php echo $message;?>>
However, $message, which comes from a MySQL table, isn't displayed correctly - any HTML output that should be in $message is broken.
How do I do this properly?
This will prevent your tags from being broken by the echo:
<?php echo htmlentities($message); ?>
If you want to display it
echo htmlspecialchars($messge, ENT_QUOTES, 'UTF-8');
That's what I usually do.
Since the answers are difference:
htmlentities-vs-htmlspecialchars is worth checking out.
I normally use the following code, see htmlspecialchars
<?php echo htmlspecialchars($videoId, ENT_QUOTES | ENT_HTML5); ?>
whats wrong with using a constant ?
<?php
define(foo,'<,>,?,","".~,,!##$%^&*()_+}{":?<<>,./;');
$foo2="'[.]";
echo constant('foo').$foo2;
?>
you need to put the '[.]' into a variable, as a constant will break on a ' (single quote).

Categories