Quick php/MySQL syntax question!

Associate
Joined
15 Jan 2003
Posts
1,117
Location
Bristol/Manchester Uni
Hey,

I'm new to php/MySQL and am currently working through a php/MySQL tutorial/book... I'm getting along well with it, but one thing that is bugging me is part of the syntax the book tells me to add in, yet has not explained it at all!.. So I was thinking maybe its something really insignificant, but for when I'm writing my own scripts from scratch I would really like to understand WHY I'm including each part, and the necessity for it etc..

So anyways what I want to know is what the ' . ' means.?

For example:

PHP:
$dbcnx = @mysql_connect('localhost', 'root', 'mypassword');
 if (!dbcnx) {
   exit('<p>Unable to connect to the ' .
        'database server at this time.</p>');
}

So in the above connection to the MySQL server I'm adding a custom error message, BUT what and why has the tutorial/book told me to add ' . after 'connect to the' .. then ' before the 'database server at...' etc.

If anyone could clear this up, it would be great!.. as I have no idea as to why its used, and what it does etc.. I've also used it when inserting php functions within an error message, such as..

PHP:
Error: ' . mysql_error() . '

Any help on clearing this question up, would be great!

Thanks, ;)
 
It's the concatenation operator. No idea why they used it to connect a string to a string, though...

It's used to join things together.

Say..

$foo = "hello";
$bar = " there";

$foobar = $hello . $there;
echo $foobar;

//produces "hello there"

You also use it with single quoted strings because single quoted strings don't print the value of functions or varaibles. So if you just put mysql_error() inside the single quotes you'd literally have the text "mysql_error()" written to the screen. So you end the single quote and use a full stop then mysql_error() then another full stop and an opening single quote to carry on the string.

Hope that's sort-of clear and not too garbled :)
 
Thats much clearer thanks a lot!.. Makes sense, although I to can not understand why in the majority of the examples it has the concatenation operator in place to join two text strings..

The mysql_error() also makes more sense now, as I can understand that putting it within the single quotes would not output its value..

Thanks again. ;)
 
Probably to increase legibility by being able to throw the string onto a second line, which in turn may infact be due to limitations of line length in the book. Either way, they should've explained it better by the sound of it :)
 
Back
Top Bottom