question about operator in php

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
iansane
Forum Commoner
Posts: 62
Joined: Sun Apr 18, 2010 1:26 pm

question about operator in php

Post by iansane »

Hi,

I was wondering if someone can explain this line of code to me.
Specifically the meaning of ".="
I found it at w3schools but can't make any sense of it's purpose or usage so I was thinking an explanation of this sample would help me understand.

Code: Select all

$sql .= "validation2 = '".$validation2."',";
Understanding what it means might help me understand how it actually does anything when run with mysql_query($sql);


Thanks
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: question about operator in php

Post by califdon »

.= is the string concatenation operator. It is used to append a string to another string.
iansane
Forum Commoner
Posts: 62
Joined: Sun Apr 18, 2010 1:26 pm

Re: question about operator in php

Post by iansane »

Thanks califdon.

I had to think about it for a minute and look at the rest of the code but I see now it's adding to the end of a $sql statement based on "IF" statements

One more question if you don't mind.

I'm getting a lot of errors in this code I'm debugging because when they insert a variable they do it like this

Code: Select all

$Sql = "INSERT INTO TABLE (fname) VALUES ('". $fname ."');";
Instead of this

Code: Select all

$sql = "INSERT INTO TABLE (fname) VALUES ('$fname');";
I'm not sure what key words to search for to find it on the Internet but I've never seen that before and a single quote works for me. Is it some deprecated or new way of inserting php vars in statements?

Thanks
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: question about operator in php

Post by califdon »

In the case of a simple variable like that, you don't need to break out of the double-quoted string. I guess some programmers just form the habit of doing that because more complicated values, such as arrays and function calls, are NOT interpreted within double-quoted strings. For example, if you needed to include a POST variable in an SQL string (which is a risky thing to do, I'm just using it as a syntax example--don't ever do that!), you could NOT do this:

Code: Select all

$sql = "SELECT * FROM myTable WHERE id = $_POST['id']";
That would produce an error. You would have to do it either like this:

Code: Select all

$sql = "SELECT * FROM myTable WHERE id = ".$_POST['id'];
or like this (better, but it's still bad to use a POST variable directly, without "sanitizing" the value to prevent a hacker from slipping in code that would give him access to your database):

Code: Select all

$id = $_POST['id'];
$sql = "SELECT * FROM myTable WHERE id = $id";
In fact, the CORRECT way to do that would be:

Code: Select all

$id = mysql_real_escape_string($_POST['id']);
$sql = "SELECT * FROM myTable WHERE id = $id";
iansane
Forum Commoner
Posts: 62
Joined: Sun Apr 18, 2010 1:26 pm

Re: question about operator in php

Post by iansane »

Thanks again. It was confusing and I don't know why but with the "or die(mysql_error))" it was dying and telling me I had a syntax error for every time ".$var." was used. I have the latest version of php and mysql so maybe that way has been deprecated for regular variables? I don't know but '$var' works.
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: question about operator in php

Post by califdon »

iansane wrote:Thanks again. It was confusing and I don't know why but with the "or die(mysql_error))" it was dying and telling me I had a syntax error for every time ".$var." was used. I have the latest version of php and mysql so maybe that way has been deprecated for regular variables? I don't know but '$var' works.
No, the error you received was because you didn't use single quotes within the double quotes in the first method. Evidently your id field is text, not numeric (somewhat unusual, id's are usually numeric), so they must be surrounded by either single or double quotes. Since your entire SQL variable is enclosed in double quotes, the value of any text (varchar) field must be enclosed in single quotes (well, technically you could also use escaped double quotes), so the first method would have to be done like this:

Code: Select all

$Sql = "INSERT INTO TABLE (fname) VALUES ('". $fname ."');";
Notice the single quotes that are "inside" the double quotes (inside, from the standpoint that they are within the double quoted string)?

This is all part of the excruciating detail that all computer language syntax requires you to learn. Boring, but absolutely necessary!
iansane
Forum Commoner
Posts: 62
Joined: Sun Apr 18, 2010 1:26 pm

Re: question about operator in php

Post by iansane »

OK, sorry when I typed ".$var." I was referring to the '". $var . "' method but forgot to put the double quotes in.

It was lines with exactly the method you post above that were throwing the errors and when I took out the dots and double quotes like '$var' it worked.

It could be that other things were causing the error as there were many variables and some concatenating query strings that grow to be very large if the right conditions are met in the "IF" statements. Maybe there was a missing or extra quote or double quote somewhere and it was coincidence that taking them all out but the single quotes eliminated the problem.

That's a lot of good information though as I will probably need it for other queries and tasks.

Thanks
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: question about operator in php

Post by califdon »

Oh, OK, you are probably right, then, about some other tiny oversight causing the error. That frequently happens and the only solution is to be r-e-a-l-l-y careful, especially when you are entering a complex expression. I kick myself all the time because I omitted a closing parenthesis or something, where there are several in a group, like:

Code: Select all

if(something(this)==strtolower($_POST['that'])) { 
A tip for fast checking parentheses, by the way, is to count all the opening parentheses in a command, then all the closing ones. They must be equal. Like above there are exactly 3 of each.
Post Reply