This question already has answers here:
How can I combine two strings together in PHP?
(19 answers)
Closed 7 years ago.
Googled but couldn't find an answer.
I have
echo "<input type = 'text' value = ".$value." name = ".$input_id."/>";
$value or $input_id contains a dot which is conflicting with the dot used for concatenation. How do I escape it?
Thanks!
If the variables contains eg. space, they has to be in quotes. Concat operators cause no problem.
echo "<input type='text' value='" . $value . "' name='" . $input_id . "'/>";
Use this
echo "<input type='text' value='" . $value . "' name='" . $input_id . "'/>";
Related
I have been using this syntax
print "<td>" . "$" . round($res->DollarValue) . "</td>";
Which will output something like this: $8812 I want the format to be $8,812 so I tried this
print "<td>" . "$" . round(number_format($res->AveragePrice)) . "</td>";
but that did not do the trick either.
What I am after is rounding with a dollar sign and comma, but no decimals.
Further Examples
1000.78 -> $1,001
1000.23 -> $1,000
What is the php synatx for this?
This question already has answers here:
PHP parse/syntax errors; and how to solve them
(20 answers)
Closed 5 years ago.
I can's seem to figure out why I'm getting this error.
I need to turn user_link part into hyperlink(right now it outputs a text on the frontend). Error happens on this line in my shortcode.php file. I guess there is an error with a href part:
$output .= '<td>' . $order->billing_first_name . ' ' . $order->billing_last_name . ' <a href='" . $order->user_link . "'>" . $order['user_link'] . "</a> </td>';
Your issue is right here:
. ' <a href='" .
Notice how you're starting with a single quote and ending with a double.
Definitely consider using an IDE. It will make these easy bugs blatantly obvious. I highly recommend PHPStorm, or if you don't want to spend anything, sublime text (Not an IDE but will provide linting and highlighting).
On top of that I'd recommend using some type of templating engine eventually. You should always try to avoid writing HTML as strings.
$output .= '<td>' . $order->billing_first_name . ' ' . $order->billing_last_name . ' ' . $order['user_link'] . ' </td>';
You switched the ' and " in the a href. I also changed the two " to ' in the a description.
Im trying to pass a variable in the URL and to post the variable on the page. I currently have a table and a variable named $record['Filename']. The value is not displaying correctly.
Right now I have the following
while($record = mysql_fetch_array($mydata)){
echo "<tr>";
echo "<td>" ."<a href='Info.html?page='.$record['Filename'].> $record['Filename'] </a>". " </td>";
echo "<td>" . $record['Description'] . "</td>";
echo "</tr>";
}
PHP strings 101:
echo "<td>" ."<a href='Info.html?page='.$record['Filename'].> $record['Filename'] </a>". " </td>";
^--start string end string --^
Since you never "exit" your "-quoted strings, your . are just plaintext periods, not concatenation operators.
You probably want this:
echo <<<EOL
<td>{$record['Filename']}</td>
EOL;
Notice how using a heredoc removes the need to hop in/out of string mode, and you can use proper quoting in the HTML. Also note the {}-extended string syntax around the variables.
try to change,
<a href='Info.html?page='.$record['Filename'].> $record['Filename'] </a>
to
<?php echo $record['Filename'];?>
Use single quotes instead of double quotes. It's faster and more efficient.
while($record = mysql_fetch_array($mydata)){
echo '<tr>';
echo '<td>'.$record['Filename'].'</td>';
echo '<td>'. $record['Description'].'</td>';
echo '</tr>';
}
I am having a problem trying to get a row to create everytime I enter a new database entry.
My code so far is:
<table align="center">
<th>MH/s</th><th>Contact Length</th><th>Date Bought</th><th>Payment</th>
<?php while ($row_cnt > 0) {
echo "<tr><td>" . $row['mhbought'] . "</td><td>" . $row['length'] . "</td><td>" . date(d-m-Y, $row['datebought'] . "</ td><td>" . $row['payment'] . "</td></tr>";
}
?>
</table>
The error I receive however states PHP Parse error: syntax error, unexpected ';' (which is the echo line) Am I doing something completely dumb here, can this even be done?
Thank you for any help you may provide.
Looking at the code, the problematic line is this:
date(d-m-Y, $row['datebought']
^^ missing quotes ^ missing closing parenthesis
Change it to:
date('d-m-Y', $row['datebought'])
You haven't close date() right bracket and there is quotation missing for date format. Your echo should look like:
echo "<tr><td>" . $row['mhbought'] . "</td><td>" . $row['length'] . "</td><td>" . date('d-m-Y', $row['datebought']) . "</td><td>" . $row['payment'] . "</td></tr>";
This question already has answers here:
Print string with a php variable in it
(4 answers)
Closed 11 months ago.
I have a textbox named 'fname'. I need to echo out the input of this box inside double quotes on a another page.
User enters: Test123
returrns: "Test123"
so how can I do that with $_POST["fname"] ?
Or you can use:
echo "Hi how are you {$_POST['fname']} ? I am fine thanks";
If you want to use it in a string surrounded by letters, simply use curly brackets {}.
Put the $_POST['fname'] to curly brackets.
Try
<?php echo('"'.htmlspecialchars ($_POST["fname"]).'"'); ?>
There are many ways:
besides the one nyarathotep mentioned:
echo sprintf('"%s"', $_POST['fname']);
printf('"%s"', $_POST['fname']);
Use this:
echo '"' . $_POST["fname"] . '"';
Or
echo "'" . $_POST["fname"] . "'";
of course if you want to you can replace ' and " with " or ' in the code...