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.
Related
How can i prevent that PHP converts a recognized part of a string to an html-entity?
So e.g. lets say i have to concat parts together to an url, like:
echo '&' . 'section=' . '<br>';
$a = '&a';
$b = 'mplitude=';
echo "{$a}{$b}" . '<br>';
echo sprintf("%s%s", '"e', '=');
the code above prints the following:
§ion=
&litude=
"e=
instead of:
§ion=
&litude=
"e=
how can this be prevented without throwing filters on it trying to convert the symbols back to an string again?
You need using htmlspecialchars function:
echo htmlspecialchars('&' . 'section=' . '<br>');
i need to convert decimals values into unicode and display the unicode character in PHP.
so for example, 602 will display as this character: ɚ
after referencing this SO question/answer, i was able to piece this together:
echo json_decode('"' . '\u0' . dechex(602) . '"' );
this seems pretty error-prone. is there a better way to do this?
i was unable to get utf8_encode to work since it seemed to want to start with a string, not a decimal.
EDIT: in order to do characters between 230 and 250, double prefixed zeros are required:
echo json_decode('"' . '\u00' . dechex(240) . '"' ); // ð
echo json_decode('"' . '\u00' . dechex(248) . '"' ); // ø
echo json_decode('"' . '\u00' . dechex(230) . '"' ); // æ
in some cases, no zero is required:
echo json_decode('"' . '\u' . dechex(8592) . '"' ); // ←
this seems strange.
While eval is generally to be avoided, it seems strictly-controlled enough to be fine here.
echo eval(sprintf('return "\u{%x}";',$val));
echo json_decode(sprintf('"\u%04x"',$val));
this ultimately worked for me, but i would not have found this without the answer from Niet the Dark Absol
normally, when i attempt to answer my own question, some SO wizard comes along and shows me a built-in function that i should have known about. but until that happens, this is all i can think of:
$leading_zeros = null;
if ( strlen(strval(dechex($val))) >= 4 ) {
$leading_zeros = '';
} else if ( ctype_alpha(dechex($val)[0]) ) {
$leading_zeros = '00';
} else if ( ctype_digit(dechex($val)[0]) ) {
$leading_zeros = '0';
}
echo json_decode('"' . '\u' . $leading_zeros . dechex($val) . '"' );
EDIT: when trying to something similar for javaScript, the documentation tells me the format is supposed to look like "\u####' four digits. i dont know if this is similar to PHP or not.
If you have IntlChar available I'd recommend using IntlChar::chr:
var_dump(IntlChar::chr(602));
Failing that, something like the following avoids any eval/json_decode trickery:
var_dump(iconv('UTF-32BE', 'UTF-8', pack('N', 602)));
I'm sure I'm missing something obvious here: the following is echoing lat-long variables from MySQL, and the longitude variable begins with a minus sign, which prevents the echo statement from reading it and all that follows it. I'm sure there is a way to clean/escape that but just can't work it out.
echo "http://maps.google.com/maps?ll=" . $row['latitude'] . "," . $row['longitude'] . " target=_new>View in Google Maps";
This is output from a PDO query and testing passing the lat-long into Google Maps.
As I understand, it's a link?
Then, use urlencode for string.
The minus signs are not a problem. You may need to urlencode() because of the comma, but you need quotes around the URL in the href as well:
echo '<br /><a href="http://maps.google.com/maps?ll='
. urlencode($row['latitude'] . ',' . $row['longitude'])
. '" target="_new">View in Google Maps</a>';
So I have the following PHP code for a registration form:
<?php
$entries = array(
0 => $_POST['signup_username'],
1 => $_POST['signup_email'],
2 => $_POST['signup_password']);
$entries_unique = array_unique($entries);
$entries_unique_values = array_values($entries_unique);
echo " <br />".$entries_unique_values. " ";
?>
... And I'm realizing my echo syntax is wrong. How could I echo the different values of my array, without assigning a variable to each of my keys (there are a number of reasons as to why I can't do that)? I'd rather not use the r_print function as well.
Thanks in advance!
How do you want to output them? Comma-separated? Each on its own line? You have plenty of options. This should do the trick for comma-separated:
echo " <br />".implode(', ', $entries_unique). " ";
That said, be careful just outputting user input directly in HTML. This will leave you wide open to XSS vulnerabilities and invalid HTML in general. To output user input in HTML safely, you need to properly HTML encode the output. This would be preferable to the line above:
echo " <br />".implode(', ', array_map('htmlspecialchars', $entries_unique)). " ";
See implode(), array_map(), and htmlspecialchars().
Take a look at php's var_export().
var_export — Outputs or returns a parsable string representation of a variable
Try the implode() function:
echo implode(', ', array_values($entries));
he foreach loop is really easy for arrays, especially single associate arrays.
foreach($entries_unique as $key => $value) {
echo "key: " . $key . " - value: " . $value . "<br/>";
}
Check out php.net: http://php.net/manual/en/control-structures.foreach.php
Ok I need to find out what is contained inside a PHP variable and I have it to do it visually, is there a function to display whatever that's contained in a string as it is?
For example :
$TEST = ' ' . "\n" . ' ';
if I use echo the output will be :
while i want it to be :
 \n 
is it possible? (I hope I was clear enough)
ty
You can use json_encode with htmlspecialchars:
$TEST = ' ' . "\n" . ' ';
echo json_encode(htmlspecialchars($TEST));
Note that json_encode has third agrument in PHP 5.4.
var_dump() should do the work for you?
Example:
echo "<pre>";
var_dump($variable);
echo "</pre>";
Use <pre> to keep the format structure, makes it alot easier to read.
Resources:
http://php.net/manual/en/function.var-dump.php
http://www.w3schools.com/tags/tag_pre.asp
Try print_r, var_dump or var_export functions, you'll find them very handy for this kind of needs!
http://www.php.net/manual/en/function.htmlspecialchars.php
or
http://www.php.net/manual/en/function.htmlentities.php
$TEST = ' ' . "\n" . ' ';
echo htmlspecialchars(str_replace('\n','\\n', $TEST), ENT_QUOTES);
or
$TEST = ' ' . "\n" . ' ';
echo htmlentities(str_replace('\n','\\n',$TEST), ENT_QUOTES);
You may have to encode the newlines manually. If you want to encode them as actual newlines you can use nl2br. Or string replace these characters with your preference. Update: as I have added to the code per request. String replace special characters you wish to see like newlines and tabs.
assuming you want it for the debugging purposes, let me suggest to use urlencode(). I am using it to make sure I don't miss any invisible character.
The output is not that clear but it works for me.