Using double quotes instead of single quotes in DOMXpath query fails - php

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?

Related

PHP Associative Arrays - Single vs Double Quotes

Is there any difference between using single and double quotation marks for Associative arrays names in PHP ?
e.g.:
$age = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43");
$age = array('Peter'=>'35', 'Ben'=>'37', 'Joe'=>'43');
Practically no.
There is a slight difference, though. If you use double quotes, the string will be computed; embedded variables will be expanded.
Single quotes on the other side are literals. They are handled as-is.
I could imagine that the double quotes produce a slight overhead due to computing, but I doubt it's significant.
I personally use single quotes for programmatic stuff and double quotes for user-presented text, unless required otherwise.
That's a neat little rule making life easier ;-)
No, apart from the general PHP rules regarding the quotes. See also http://php.net/manual/en/language.types.string.php
No.
Single vs. double quotes in this context only matters if the thing between the quotes is a variable.
For example, if $Test = 43,
$Var["$Test"] = $Var["43"] but $Var['$Test'] = $Var['$Test']

PHP - Why single-quoted variables inside doubles are still parsed

Normally, when variables in PHP are enclosed in single quotes, they are treated as strings, i.e
echo '$variable';
will actually echo the word $variable onto the screen.
So why is it then that this string is parsed:
echo "'$variable'";
That code actually does echo the value of the variable. Why is that? It's still inside single quotes, so why does it still get parsed?
The string is wrapped in double quotes -- the single quotes are part of the content of the string, not part of the string's delimiter. Therefore the single quotes have no semantic meaning whatsoever.
Your question indicates that you may have a fundamental misunderstanding of strings. This is OK! Strings are surprisingly complex entities, and will only get more complex if you learn lower level languages like C. I would suggest you spend some time reading up on strings both in general as well as within PHP. A few quick google searches will honestly be better than a curated list for this task.
Because the single quotes are inside double quotes. Anything inside double quotes gets evaluated. So, your echo statement is passed a string inside double quotes.
This string is evaluated then output. It contains single quotes and a variable.
Try this instead:
<?php
$var = 10;
echo '"$var"';
?>
Because it's in double-quotes as well. The outer most layer of quotes denotes what kind of string it is.
It is simply a double quoted string that contains two single quote characters. Once they are in the double quotes, they have no meaning to the parser.

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

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.

learning about single and double quotes (.),("), and (')

Can you tell me what is the different using (')single quotes inside (")quotes and (")quotes inside (')single quotes? and at concat, what is the meaning of this '".$bla."' I still can not distinguish them.
In SQL, anything with single quotes is considered a text based data type.
SQL uses double quotes for escaping keywords and non-ASCII characters.
This:
'". $bla ."'
..is PHP syntax. $bla is a PHP variable, the period is a string concatenation character (which is why there's one on both sides). So in this example, the content of the $bla variable is being concatenated into a string, where it will be surrounded by single quotes.
The main difference is the anything in a double quote is evaluated and anything in a single quote is not. There has been some discussion that it is better to use single quotes than double quotes so that PHP does not need to evaluate every aspect of the line to determine if it is a variable or not:
$good = 'really good';
echo "this is not $good"; //bad
echo 'this is' . $good; //good
It just keeps thing running faster and keeps the code looking cleaner.

Categories