spacing between square brackets of an array - php

I have an array where I would like to put spaces between the [] like :
$array[South Africa]=array();
But I can't... why this is not possible?

The correct way of doing this is:
$array['South Africa'] = array();
By not placing quotes around strings, PHP will first check if it is a constant, and if not assume you want to specify the string stated (and generate a warning).
This would work without a space (other than the warning and being bad practise) but with the space PHP thinks the string/constant has ended after 'South' and expects an ]. What you have specified will result in a syntax error:
unexpected T_STRING, expecting ']'
I personally would avoid using spaces for names/keys anyway but the above explains the problem you are having if you must do this.

Related

Two equal PHP statements giving different results?

I was doing some coding on my phone when I got this error:
Parse error: syntax error, unexpected '"log.txt"' (T_CONSTANT_ENCAPSED_STRING) in your code on line 15
This is the offending code:
$file = "log.txt";
Note that you have to copy this exactly to get this result. This is because when I retype it:
$file = "log.txt";
I get no error. The two lines are the same, except for one thing: the second space in the line is binary "10100000" in the crashing statement, but "00100000" in the retyped one.
So I suppose my editor screwed up in some mystical way I will probably never discover. But what I don't get is why does this bad character look exactly like a space (as opposed to something else or a �), and why does it cause this particular error as if I didn't properly open my string?
It's a non-breaking space character.
It probably came about as a result of copying the code in from a blog post or from Word, for example.
The interpreter is not able to parse it as a regular space, hence the error.

making close button with java script in php

For closing a <div>, I use onclick="this.parentNode.style.display='none';" (in HTML) and it works. But in PHP it doesn't work!
error:
Parse error: syntax error, unexpected T_STRING, expecting ',' or ';' in /home/u381013597/> public_html/index.html on line 311
When I change 'none' to "none" there is no error but the close item doesn't work.
What is the problem?
When you print in PHP you have to watch out with strings. You define a string with single or double quotes. If your string contains for example single quotes and you defined the string with a single quote the quote in the string itself will mean end of string, so you have to escape them:
echo 'onclick="this.parentNode.style.display=\'none\';"';
As you see I used single quotes for printing here, and I escaped every single quote in the string itself with the backslash character: \
Otherwise, the string would ended before none (because there is a unescaped single quote that means the end of the string) and the parser would except a ; character that means end of command or a , character that marks that there will be other parameters, but you give none of them, it gets none instead. Thats why it throws that he get an unexpected T_STRING. If you look at your error message, you will see that it says the same as I did, just in a compact way:
error: Parse error: syntax error, unexpected T_STRING, expecting ',' or ';' in /home/u381013597/> public_html/index.html on line 311
It also says that there is a Parse error: syntax error that means you mistyped something, and he also says where does the problem occurs.
Error messages are your friends, they give a hint about the problem. Read (or at least search for) them and you will be able to develop much faster.

How does PHP naturally anticipate apostrophes in variables?

I just noticed that I could use an a variable as an argument, like this: $variable = "This's a string."; function('$variable'), and not like this: function('This's a string');. I can see why I can't do the latter, but I don't understand what's happening behind the scenes that meakes the first example work.
Have you heard about formal languages? The parser keeps track of the context, and so, it knows what the expected characters are and what not.
In the moment you close the already opened string, you're going back to the context before the opening of the string (that is, in the context of a function call in this case).
The relevant php-internal pieces of codes are:
the scanner turns the sequence between ' and ' into an indivisible TOKEN.
the parser puts the individual indivisible tokens into a semantic context.
These are the relevant chucks of C code that make it work. They are part of the inner workings of PHP (particularily, the Zend Engine).
PHP does not anticipate anything, it really reads everything char by char and it issues a parsing error as soon as it finds an unexpected TOKEN in a semantic context where it's not allowed to be.
In your case, it reads the token 'This' and the scanner matches a new string. Then it goes on reading s and when it finds a space, it turns the s into a constant. As the constant and the previously found token 'This' together don't form any known reduction (the possible reductions are described in the parser-link I've given you above), the parser issues an error like
Unexpected T_STRING
As you can deduce from this message, it is really referring to what it has found (or what it hopes it has found), so there's really no anticipation of anything.
Your question itself is wrong in the sense that there's no apostroph in the variable (in the variable's identifier). You may have an apostroph in the variable's value. Do not confuse them. A value can stand alone, without a variable:
<?php
'That\'s fine';
42;
(this is a valid PHP code which just loads those values into memory)
function('$variable') shouldn't be working correctly
Characters within the " " escape single quotes
Characters within '' do not escape single quotes (they cant escape themselves!).
Using the "" also lets you use variables as part of a string, so:
$pet = 'cat'
$myStory = "the $pet walked down the street"
function($pet) is the way the function should be passed a string
use it like this
function('This\'s a string');

PHP lines with XML cause unexpected '>' errors

When I set variables that include angle brackets (< >) or slashes I keep getting errors like the following (code simplified to focus on error):
Parse error: syntax error, unexpected '>' in D:\hosting\8499439\html\test.php on line 2
<?php
$xml = “<Request>\n”;
?>
I also run into a lot off issues with "unexpected T_String" errors that appear to be related.
I'm running PHP5 on a GoDaddy Windows Server.
What am I doing wrong? (I get the impression I need to to do something so that special characters can be handled in my PHP).
Thanks in advance.
Your quotes are curly quotes, not straight quotes, so PHP runs into an error processing them. A string can only be recognized with straight quotes.
Use the following code:
<?php
$xml = "<Request>\n";
?>
Assuming that you have the same error elsewhere, you can probably do a simple search-and-replace to fix the error: search for one of the curly quotes, replace with a straight quote. Repeat with the other curly quote. Make sure to check for straight quotes that may need to be escaped (for instance, something like "Mary said, "I like this."" would need to be escaped as "Mary said, \"I like this.\"")
mc10 is right.
Additionally I can say, there are only ""(double) and ''(single) quotes in PHP. I suggest you to read about differences between them.
I prefer using single quotes only to keep code clear.

SyntaxError: Unexpected token ILLEGAL caused by \u2028 and \u2029

I am sending a sting as a parameter to a JavaScript function:
theJSFunction('say hi dude');
Chrome gives me SyntaxError: Unexpected token ILLEGAL so after research, I've found that the following whitespaces generates the error \u2028 and \u2029.
The problem is, the string posted to the function is printed via PHP and I need it to be printed via PHP (could use ajax, but I am required to let the PHP print it).
Is there any way to remove those to characters through PHP or JavaScript?
You can replace with .replace. Passing a regular expressions with the g flag replaces all occurences, and you can include \uxxxx characters as well:
"\u2028\u2029".replace(/\u2028|\u2029/g, "").length; // 0

Categories