Money formats with commas in an array - php

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.

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']

json_encode() Not Escaping Double Commas in Certain Situations

Here's a fiddle demonstrating the problem: http://phpfiddle.org/lite/code/hd0t-ebjr
<?php
require "simple_html_dom.php";
$html = file_get_html("https://play.google.com/store/apps/details?id=com.vlambeer.RidiculousFishing&hl=en");
$test = $html->find('.id-app-orig-desc', 0)->innertext;
$data = [
'test' => $test
];
die(var_dump(json_encode($data)));
?>
Scroll down and you'll notice that all the double quotes are not escaped correctly. Although other characters are ("/" for example).
The weird thing is, that I can't seem to reproduce it when copying the test string into the php code. Only when loading it from the url.
Any idea what could be happening here?
These double quotes are not really double quotes. They are just entity html, ", no need to escape it.

Proper use of double & single quotes, concatecating periods, and commas

Pretty new to PHP, trying to figure out proper syntax for concatecating variables and such into strings.
For example:
A $mydir = "../../uploads/images/'".$id."'/thumb";
B $mydir = "../../uploads/images/".$id."/thumb";
C $mydir = '../../uploads/images/'.$id.'/thumb";
D $mydir = "../../uploads/images/$id/thumb";
Which one is correct?
What about when you end a string with a variable, but have to comma out to define the next element?
mkdir('../../uploads/images/' . $newid , 0777);
What about when the variable is in the middle?
mkdir('../../uploads/images/' . $newid . '/thumb', 0777);
Lastly, can anyone recommend a good resource for PHP reference? W3Schools isn't cutting it...
Strings in PHP can use either double or single quotes. There is a difference between the two, in that using double quotes will cause PHP to interpolate any variables in the string. For instance:
$var = 'test';
echo "This is a $var"; // outputs: This is a test
echo 'This is a $var'; // outputs: This is a $var
Because of this, using double quotes around your strings is a bit slower, since the string must be interpolated by PHP before it can be output. There is also nowdoc and heredoc support for strings in PHP, as well.
Aside from that distinction there is no difference and you can use them interchangeably, as in the following example:
echo 'I like ' . "concatenating" . ' strings';
It is probably a good idea, though, to be consistent throughout your code. For more information, please refer to the manual
Go to the PHP Manual: http://php.net/manual/en/language.types.string.php
As for the different types of strings:
If you use the double-quoted strings, you can include variables inside of the string like this:
$name = "world";
print("Hello $name");
Single Quotes will not expand variables.
The period is just the concatenation operator. So if you end by concatenating a variable that's fine. I.e. this is ok:
$name = "world";
$greeting = "Hello ".$name;
You shouldn't use your A or B, if you have double quotes, using D is much nicer to read. That is not to say you can't use it, if you like having a hard time reading your strings, go ahead!
The comma after the string doesn't matter
mkdir('../../uploads/images/' . $newid , 0777); // works
mkdir('../../uploads/images/' . $newid . '/thumb', 0777); // works too
mkdir("../../uploads/images/$newid" , 0777); // works and is nicer to read
mkdir("../../uploads/images/$newid/thumb", 0777); // also nicer to read
If the value you want in the string is not a variable, you either have to create a variable, or you have to use regular string concatenation (instead of interpolation)
B and D are correct. The only difference between single and double quotes in PHP is that the content between double quotes is parsed for PHP. From php.net,
When a string is specified in double quotes or with heredoc,
variables are parsed within it.
A - has a pair of unnecessary single quotes.
B - FINE
C - has an incorrect ending quote. should end in a single quote.
D - FINE
for concatenation B or C will both work, however for relative file paths it's usually best to use the
$_SERVER['DOCUMENT_ROOT']
syntax, and access your files relative to your server's html root folder, meaning your syntax will look something like
$_SERVER['DOCUMENT_ROOT']."/folder/foler/".$id."/thumb";
A won't do it.
B is the best.
C has a syntax mistake. Moreover, for strings you generally use ", but on the other hand, ' is used when formatting html like: 'Google!' so you don't need to escape quotes and the code looks nice.
D works, but not recommended. For example in D `"blah $this -> name blah" won't work. That is the reason.
from your choice list, 'B' is fine, so is 'D'. My favorite reference is the official manual: http://www.php.net/manual/en/

How can I write a square bracket to a text file in php?

I'm trying to write a php script that will generate a variety of new php pages, but I'm finding that I'm unable to write a square bracket out. When I escape a square bracket in the same way as other characters (ie [ ) the leading \ is written to the new page, which results in code that doesnt work:
echo $row\['Value'\];
When I do not escape the bracket, the page fails, and the same thing happens when I try and substitute asc(91).
I have seen other examples that use code like $row->Value, but I tried that and it didn't work. If anyone can help me output a square bracket, or knows of another method by which I can fetch a value from a row without using one at all, I'd be very grateful
Your echo would appear as an array reference to PHP. Try this:
echo $row, "['Value'];"
assuming that you want the value of $row to be output, and not the literal text $row. If you want the literal text, (e.g. you're trying to build a PHP script on the fly), then either of these should do the trick:
echo '$row[\'Value\'];';
echo "\$row['Value'];";
How about this:
echo sprintf("\$row['%s']", $value); // either scenario
echo sprintf("%s['Value']", $row);
Keep in mind that PHP automatically parses double quote strings ("), and tries to find variabels within. So, the bracket is probably not the issue, the $ variable prefix (coupled with the parser) probably is.
There are a couple other answers that work but I want to elaborate:
The "echo" construct can take a variable or a string. You can't echo a string to the screen in the same way that you do a variable. For example: echo hello; will not behave as you might think. You need to include it in quotes such as echo "hello";
You can also use single quotes. Single quotes and double quotes behave differently. For example:
$foo = "bar";
echo $foo;
echo "$foo";
echo '$foo';
The first will echo "bar", the second will also echo "bar" because PHP looks for variables in double quotes strings. The third will echo '$foo' because PHP does not try to do variable substitution in a single quoted string. So you can do (as #mark-b said):
echo "\$row['Value']";
or
echo '$row[\'Value\']';
Now, that $row->value syntax that you saw, is object notation. It is assuming that $row is an object and not an array. Objects are a whole other ballgame.
You're talking about code generation in your question, so I expect you also want to output the 'echo' statement in the generated code. Assuming you want to save the output into a file so it can be easily executed, you want to use something like fwrite or file_put_contents, I expect. You need to think in terms of strings, which can be a bit tricky when you're seeing code.
Something like this should work:
fwrite($fp, 'echo $row[\'Value\'];'."\n");
Note how the single and double quotes work. \n is resolved to a newline, but anything in the single quotes is treated as a string and is printed as is, apart from \', which should print a literal single quote in the output file.
Hope this helps.

basics of php strings

//i am really very confused about what is used to represent a string in php i.e. if we use double inverted commas it too represents string and same if use single inverted commas
so "avinash" or 'avinash'........which is a string?
//and plz can u tell me about a good book to read php5 from
Double quotes (") and single quotes (') both represent strings. However, PHP doesn't treat them the same way.
In double quoted (") strings, escape sequences and variable names are expanded. In single quotes (') strings, they are not. The string is not expanded.
So, given the following:
$name = "Foo";
The following code...
$doubleQuotedString = "Hello $name.\nIt is nice to see you.";
echo $doubleQuotedString;
... will output this:
Hello Foo.
It is nice to see you.
However, the following...
$singleQuotedString = 'Hello $name.\nIt is nice to see you.';
echo $singleQuotedString;
... will output this:
Hello $name.\nIt is nice to see you.
For more information, you can read the following documentation page on strings.
PHP Documentation: Strings

Categories