php error with post inside variable - php

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;

Related

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

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]";
// ^ ^

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?

Syntax and Variables in PHP

I'm having some trouble with writing some syntax. I want to echo this
'location_1' => $location_1,
However, it is not as simple as it seems. When I write the echo statement the integer 1 must be the variable $z. Here is the code I attempted to write
echo "'location_' . $z . '' =>' . ${'location_' . $z} . ','";
This is what it outputted
'location_' . 1 . '' =>' . something . ','
$location_1 is equal to the string something. I'm lost at how to do this the right way. Any guides on describing how this syntax works would be a major help too so I can understand it completely.
You can just write variables directly into double quoted strings see http://www.php.net/manual/en/language.types.string.php#language.types.string.parsing
echo "'location_$z' => \$location_$z,";
You might want to also read the rest of the strings doc
This is the link to the echo documentation (see the examples, I think they described well how it works)
You can break it into two lines and get the expected output.
For example:
$var_location = "$". "location". $z;
echo "'location_" . $z . "' =>'" . $var_location . "','";
One way is: echo "'location_{$z}' => \$location_{$z},";
Edit: Is this what you meant?
<?php
$z = 1;
$location_1 = 'something';
echo "'location_$z' => " . ${'location_'. $z} . ',';
which produces: 'location_1' => something,
Why don't you store these variables inside an array for easier access. Something like:
$locations = array('location_id' => 'location_name');
Here's one way:
echo "'location_$z' => \$location_$z,";
You need to escape the $ symbol. The double quotes represent the thing to echo in this case, whereas the single quotes actually get echoed.

Separate quotes from string

The following code keeps giving me this error
Parse error: syntax error, unexpected T_VARIABLE in ...
?
$query_string = 'this is a test... "this is in quotes" mmm..chicken burgers... yummm...';
preg_match_all("/\".*\"|[^\s]*/", ­ $query_string, $matches);
echo "Matches:";
foreach($matches[0] as $token) {
echo $token . "<br />";
}
it is from this web page
Have you looked at the line referred to in the Error Message you noted?
Have you looked at the lines preceding that line, to ensure that you have ended each line with the semi-colon ";", that you have used the correct operators for joining variables ".", etc.?
This sounds like a simple PHP Syntax error.
I just ran the following code on my XAMPP server with no error messages apparent:
<?php
$query_string = 'this is a test... "this is in quotes" mmm..chicken burgers... yummm...';
preg_match_all("/\".*\"|[^\s]*/", $query_string, $matches);
echo "Matches:";
foreach($matches[0] as $token) {
echo $token . "<br />";
}
As Col. Shrapnel noted, you have hidden dash symbol (173 decimal, Hex 00ad) in your code right before $query_string. Remove that and you'll be much better.
Update: to be exact, you have [comma], [space], [space], [hidden dash], [space], '$query_string'.

PHP Calculations inside string creation

I want to to create a string in php, but within the creation of the string I want to do a small calculation, here is an example of what I am trying but does not work.
$eggs = 2;
$breakfast = 'I ate '. $eggs-1 . ' eggs for breakfast today!';
Using PHP like this gives me a unexpected T_CONSTANT_ENCAPSED_STRING error.
You need to add brackets:
$breakfast = 'I ate '. ($eggs-1) . ' eggs for breakfast today!';

Categories