Why isn't my variable expanded in this string? - php

$q = $_GET['q'];
// Load and parse the XML document
$rss = simplexml_load_file('http://search.twitter.com/search.atom?lang=en&q=$q&rpp=100&page=1');
This returns results containing "$q" instead of results containing the value of $q. Why won't it work?

Change your quotes to double quotes in the simplexml_load_file line
$q = $_GET['q'];
// Load and parse the XML document
$rss = simplexml_load_file("http://search.twitter.com/search.atom?lang=en&q=$q&rpp=100&page=1");
PHP will automatically resolve the variable only if the string is in double quotes.

You need to use double quotes to have variables getting expanded:
Variable parsing
When a string is specified in double quotes or with heredoc, variables are parsed within it.
So:
"http://search.twitter.com/search.atom?lang=en&q=$q&rpp=100&page=1"
But it would be even better if you use proper URL escaping with urlencode:
'http://search.twitter.com/search.atom?lang=en&q='.urlencode($q).'&rpp=100&page=1'
You can also do that when declaring the $q variable and then use the double quoted string delcaration:
$q = urlencode($_GET['q']);
$rss = simplexml_load_file("http://search.twitter.com/search.atom?lang=en&q=$q&rpp=100&page=1");

Use double quotes instead of single quotes.

Related

PHP close tag concatenate confusion

eval("\$data = $myvar('https://www.example.com/json/id='". $_GET['name'] ."'))';
This got me an error, how to concatenate it properly?
Eval is dangerous, but for your specific question above, the quotes were off at the end, and myvar is supposed to be a function. See below:
eval('$data = myvar("https://www.example.com/json/id='. $_GET['name'] .'");');
If you use double quotes like "$data" then $data will first be evaluated and the result or value will be eval'd instead. This is one of the risks of using eval(). If you use double quotes, then escape the $ signs like so:
eval("\$data = myvar('https://www.example.com/json/id=". $_GET['name'] ."');");
Demo: IDEOne
eval("\$data = $myvar('https://www.example.com/json/id=". $_GET['name']."');");

How to add dynamically number of posts in a query in wordpress

I have the following query in my slider
<?php query_posts( 'category_name=Uncategorized&posts_per_page=3' );
and instead of 3 i need to add dynamically the posts_per_page.
For triggering an option for my functions.php theme i usually do
<?php $settings = get_option('mytheme_options'); echo $settings['postspage'];?>
I have tried
<?php
query_posts('category_name=Uncategorized&posts_per_page=$settings["postspage"]');
?>
and it doesn't do anything or echo any error
$postsPage = $settings["postspage"];
query_posts('category_name=Uncategorized&posts_per_page='.$postsPage);
The reason is that you're using single quotes. If you use single quotes, PHP will not parse the string, and as such will not replace any variables it finds with their values, it is taken as a literal string.
You can use double quotes, and change the double quotes in the array square brackets to single quotes, or you can just use string concatenation which is the better option:
query_posts('category_name=Uncategorized&posts_per_page='.$settings['postspage']);
Use double quotes where you need the string parsing, and single where you do not. You do not need to parse inside the square brackets, so just use single quotes there too.

Using a variable in $html = file_get_html();

i can't understand why variables are not working here. Here is the code:
include_once('../simple_html_dom.php');
$url = htmlentities($_GET['q']);
$urlall = str_replace(" ", "+", $url);
$html = file_get_html('http://www.example.com/some?key=$urlall&hl=en');
echo $html->plaintext;
if you look at this code you will found $urlall variable which i have applied in the web address, but this variable can't extract its data. As a new PHP programmer can't understand what to do now to make it works.. Here i have used HTML DOM PARSER..Thanks
Strings inside single quotes are literal, so $urlall is just a string, it won't be replaced with accual variable value. What you want to do is to use double quotes:
$html = file_get_html("http://www.example.com/some?key=$urlall&hl=en");
For futher explanation refer to PHP Strings:
Note: Unlike the double-quoted and heredoc syntaxes, variables and
escape sequences for special characters will not be expanded when they
occur in single quoted strings.
PHP Variables are not parsed and replaced in single quotes.
Try
$html = file_get_html("http://www.example.com/some?key=$urlall&hl=en");
Try to replace single quotes with double:
"http://www.example.com/some?key=$urlall&hl=en"
or use string concatenation instead of direct variable input into string:
'http://www.example.com/some?key='.$urlall.'&hl=en'
Second variant is more preferable.
I hope this helps you

Using double quotes instead of single quotes in DOMXpath query fails

This might just be a problem with the php syntax, but why does this work:
$b = new DOMXPath($z);
$b = $x->query('//div[contains(#class,"xxx")]');
but using double quotes with single inside does not:
$b = $x->query("//div[contains(#class,'xxx')]//a");
Maybe it has something to do with the single quote inside the string.
Try this:
$b = $x->query("//div[contains(#class,\"xxx\")]//a");
Otherwise I would just stick to the single quote version. Whats the big deal anyway?

Money formats with commas in an array

My PHPstorm is throwing a wobbly with the formats of an array. Surprsingly I've found no direct answer to how to have this array formatted. I've tried the following, I'm surprised the single quotes don't work and then the other two but no luck...
$array = array(’$2,000,000’,’$3,000,000’,’$4,000,000’);
$array = array("$2,000,000","$3,000,000","$4,000,000");
$array = array("\$2,000,000","\$3,000,000","\$4,000,000");
The manual doesn't have commas as escapable. Given that the array is for HTML ouput only I could put
$array = array("&#362&#44000&#44000","&#36$3&#44000&#44000","&#364&#44000&#44000");
but i want to LEARN HOW TO DO IT properly!
The single quotes don't work because what you have here are NOT single quotes, but rather curly apostrophes:
// Incorrect - not real single quotes:
$array = array(’$2,000,000’,’$3,000,000’,’$4,000,000’);
// Correct single quotes:
$array = array('$2,000,000','$3,000,000','$4,000,000');
Assuming you may have copy/pasted this from somewhere, always beware curly quotes when working with code. Some CMS' and frameworks will convert them for display purposes, but doing so breaks the code for copy/pasters.
You're using incorrect quotes:
$array = array(’$2,000,000’,’$3,000,000’,’$4,000,000’);
^-- ^-^--- ^---etc....
Those aren't proper quotes, and should be a ' or " character instead.

Categories