Displaying the number of users online and the page they are browsing - php

When I open the page it returns :
Parse error: syntax error, unexpected T_VARIABLE, expecting ',' or ';' in /home/a1361025/public_html/9/9/functions.php on line 44
and this is line 44
echo "$count. ($useronline[ip]) Browsing page: $useronline[page]";

Just try with:
echo $count . ' (' . $useronline['ip'] . ') Browsing page: ' . $useronline['page'] . '';

You have to escape your quotation marks, if you dont want them to end your string:
echo "$count. ($useronline[ip]) Browsing page: $useronline[page]";

You end the string and then paste a variable straight afterwards:
echo "$count. ($useronline[ip]) Browsing page: $useronline[page]";
// ^^^^^^^^^^^^^^^^^^^
You need to escape them with a backslash (because you use double quotes inside of double quotes):
echo "$count. ($useronline[ip]) Browsing page: $useronline[page]";
// ^ ^

Related

How to handle special characters like semi colons and quotation marks in php

So I've created a CMS that takes text input. This is how the data is used when I grab it from the database.
echo "<img src=" . $row['image_url'] . " alt=" . $row['caption'] . ">";
Now the problem is, whenever there's a comma or a semi colon, php treats it as part of the code and the page ends up either not rendering well or completely breaking with errors like
Parse error: syntax error, unexpected '=' in
I've tried using htmlspecialcase() when posting the data to the MySQL database but it didn't fix the problem.
EDIT: The main problem is with the alt part not the src part.
When you're using double quotes (") to create a string literal and in that string literal you want to use double quotes ("), then you may escape those double quotes (") to form a valid string.
echo "<img src=\"" . $row['image_url'] . "\" alt=\"" . $row['caption'] . "\">";
You can try like that:
<img src="<?php echo $row['image_url'];?>" alt="<?php echo $row['caption'];?> ">

Why differences in output using SUPERGLOBAL in PHP?

I am getting error by running following code:
<?php
//superglobal.php
foreach($_SERVER as $var=>$value)
{
echo $var=>$value.'<br />'; //this will result in to following error:
//Parse error: syntax error, unexpected '=>' (T_DOUBLE_ARROW), expecting ',' or ';' in
//C:\xampp\htdocs\w_j_gilmore\CH_03_PHP_BASICS\superglobal.php on line 6
}
?>
And following code runs successfully
<?php
//superglobal.php
foreach($_SERVER as $var=>$value)
{
echo "$var=>$value<br />";
}
?>
Printing in single quotation and double quotation is difference.
WHY?
The difference between the 2 is that in the first you are trying to use the => operator (which is not valid in that place, so will result in a syntax error), while in the second you print a string which happens to have the characters = and > in it.
The second effectively could be rewritten as:
<?php
//superglobal.php
foreach($_SERVER as $var=>$value)
{
echo $var . "=>" . $value . "<br />";
}
?>
If you are just trying to output $_SERVER for debug reasons, I suggest using var_dump or print_r
var_dump($_SERVER);
You have not quoted the string:
echo $var=>$value.'<br />';
quoted would look like this:
echo '$var => $value <br />';
if single quoted, variables are not interpreted.

PHP echo integer value within div tag

echo "<td>".$value["rating"]."<div class=ratingOutput data-average=".$rates." **data-id="$value["id"]"**></div>".$newline.$rateHere.$starOutput."</td>";
I want data-id to be and integer value, but cannot set it to any int for example even if i change the above code to this
echo "<td>".$value["rating"]."<div class=ratingOutput data-average=".$rates." **data-id="4"**></div>".$newline.$rateHere.$starOutput."</td>";
I get an error as such:
syntax error, unexpected '$value' (T_VARIABLE), expecting ',' or ';'
You're having an error concatenating of your string.
echo "<td>".$value["rating"]."<div class=ratingOutput data-average=".$rates." **data-id=". 4 . "**></div>".$newline.$rateHere.$starOutput."</td>";
Change this:
echo "<td>".$value["rating"]."<div class=ratingOutput data-average=".$rates." **data-id="4"**></div>".$newline.$rateHere.$starOutput."</td>";
To this:
echo '<td>'.$value['rating'].'<div class="ratingOutput" data-average="' . $rates . '" data-id="' . $value['id'] . '"></div>'.$newline.$rateHere.$starOutput.'</td>';
The best way when working with html in PHP is to use single quotes because normally you will be using double quotes for your attribute, by then you will need to escape the double quote. for the attributes you will be using.
Whenever I build HTML in PHP code, I use the heredoc syntax:
echo <<< EOT
<td>
{$value['rating']}
<div class="ratingOutput" data-average="$rates" data-id="4"></div>
$newline
$rateHere
$starOutput
</td>
EOT;
...something like that.
So you don't have to worry about escaping your double-quotes or concatenation. You can format your HTML so that it's easier to read (perhaps you don't need that $newline variable, if it's just a newline character, I can't tell).
(It doesn't have to be used with echo either, you can use it in variable assignment $html = <<< EOT - no whitespace after your token or it will fail!
If you need the last new-line character in your variable then you need a blank line before your closing token, because the closing character sequence is actually \nEOT;\n.
Many examples in articles use EOT as the token, but I abandoned that for _ a long time ago, shorter.

PHP echo and use of &

I have a variable specified earlier in my script and using the & character, otherwise the script errors out with EntityRef: expecting ';' when using a & character:
$url = "http://www.domain.com/residential?frame=RESI&MLNumber=";
The problem is that later in the script I use this variable like:
echo '<link>' . $url . '' . $title . '</link>';
The problem is that the end result is a bad URL like this:
http://www.domain.com/residential?frame=RESI&base=frames&MLNumber=26524589
How do I resolve this?

php error with post inside variable

Trying to do this:
$product_description = 'Credit balance of €' $_POST['creditamount'] ;
Want to display that post inside my variable but get this
Parse error: syntax error, unexpected T_VARIABLE in /var/www/account/credits/moneybookers/process.php on line 48
What am i doing wrong??
You are missing the concatenation operator .
Your code should look like this:
$product_description = 'Credit balance of €' . $_POST['creditamount'];
You miss a dot behind 'Credit balance of €'.
In PHP, you join strings using .. Ex.:
$new_string = $string_one . "whatever" . $string_two;

Categories