September 3, 200719 yr Hi All! Im having a bit of trouble working out when and where to use single, double quotes. I have my code: $position = "1"; $position = $position+1; echo'<td>'$position'</td><td><span><a href="viewuserall.php?id=' .$myrow['user_id'] .'&sort=1" target="_blank">' .$myrow['fname'] .'</td><td><a href="viewuserall.php?id=' .$myrow['user_id'] .'&sort=1" target="_blank">' .$myrow['lname'] .'</td><td>'. $myrow['email'] .' </span></td>'; My problem is with the first bit of code: '$position' im gettign the error unexpected T_VARIABLE, expecting ',' or ';' I understand using the '.' to join variables, im using single quotes to echo the table?????????? Thanks!
September 3, 200719 yr hey there ... quite new to PHP i see. Well lets go through all this line by line... $position = "1"; effectively you are declaring an integer (number) as a string by putting the wings around it, and even worse you are using the double qoutes... Double qoutes should be used only when you are returning a variable inside the string, eg "my name is $name" would be valid (prints out maybe "my name is Richard"), whereas 'my name is $name' would print out "my name is $name" (without replacing the variable). So the proper code for that line would be: $position = 1; no qoutes at all - just a number to show its an int - you could force it and typecast with the (integer) keyword but it makes no odds. Next line: $position = $position+1; valid, but it can be shorter. use ++ instead $postion + 1 and you have as your new code: $position++; does exactly the same, but much faster (you can also use -- for taking away 1, or += n to add n onto it) finally the echo line, this is where your error is coming from... heres the first couple of words: echo'<td>'$position'</td> ... anyone else notice 2 problems in the first 11 characters? Firstly, there is no seperation between echo and ' ... this is actually an E_NOTICE error, and may clog your error reports (but won't stop the execution). The E_ERROR-unexpected-variable you are getting is because you missed out a dot, between the ' and $, and again between n and '. the code should be: echo '<td>'.$position.'</td> ... so a couple of keynotes for the whole family to take away from this: * always use single quotes for strings, or always use double quotes - it reduces confusion (single quotes are better). * don't use strings to declare tihngs other than strings - it will work but it is messy. * remember to dot the ... places where dots go hope this was help.
September 3, 200719 yr Author Thanks for clearing that up, GREAT! Im learning from a book which didnt explain single/double quotes well! (maybe just me!) I know you use '.' to join strings together, but why do you need to have two of them, before and after the string? '.$position.'
September 3, 200719 yr the dot means "append to". So what you effectively have is a.b.c, or "a and b and c". You can write it as $a.$b."my string", or however you want, but basically the dot seperates individual entities. A variable and a string are both entities, it is just that the variable is defined prior to output, whereas the stirng is defined in the echo. If you didn't have the . then the parser wouldn't know where a variable name stops... for example this code: $a = "seven" $b = "ty"; $aty = "giggle"; $seventy = "forty four"; print $a.$b; // prints "seventy"; ( $a followed by $b ) print $a$b; // prints "giggle" (referencing variable $aty, as $b = ty) print $$a$b; // prints "forty four" ( prints the variable named "seventy", defined by variables $a and $b) ok this has probably confused you even worse... but it can be useful. basically, the string is needed and there is no way around it - it is one of the core language constructs.
September 3, 200719 yr You need the . between each piece of string. $foobar = 'foo' . $bar; 'foo' is one piece and $bar is another. Consider the . as glue, you only use it between the pieces you join. With double quotes you can include variables inside the string. $var = $world; echo "hello $var"; This will output "hello world" This doesn't work with single quotes. $var = $world; echo 'hello $var'; This will output "hello $var" as the string isn't parsed. Because single quotes isn't parsed they are faster than double quotes and you should always use single quotes when possible. Note that with array values you need to enclose them in curly brackets. $arr = Array('world' => $world); echo "hello {$var['world']}";
September 3, 200719 yr the dot means "append to". So what you effectively have is a.b.c, or "a and b and c". You can write it as $a.$b."my string", or however you want, but basically the dot seperates individual entities. A variable and a string are both entities, it is just that the variable is defined prior to output, whereas the stirng is defined in the echo. If you didn't have the . then the parser wouldn't know where a variable name stops... for example this code: $a = "seven" $b = "ty"; $aty = "giggle"; $seventy = "forty four"; print $a.$b; // prints "seventy"; ( $a followed by $b ) print $a$b; // prints "giggle" (referencing variable $aty, as $b = ty) print $a$b; // prints "forty four" ( prints the variable named "seventy", defined by variables $a and $b) ok this has probably confused you even worse... but it can be useful. basically, the string is needed and there is no way around it - it is one of the core language constructs. I didn't know you could do things like that :-) Never had a use for it before anyway but it's good to know.
September 3, 200719 yr good if you for instance make a registry class where you store arrays and access them as objects.
September 3, 200719 yr Author Cool, echo '<td>'.$position.'</td> I see the two ‘.’ Before and after the string joins the information between the <td></td>, so it reads as one. Aswell as defining the start and end of the $position string? Is that right?
September 3, 200719 yr It's best to imagine them as seperate entities. '<td>' is a string entity as is '</td>' and $position is an integer entity. You need a dot between each entity to glue it together. The dot basically says then is the end off one entity and the start of the next one. If it wasn't html you could as mentioned enclosed it all in double quotes and used {} to highlight a variable like echo "<td>{$position}</td>"; but with html you usually have double quotes in the code so it doesn't work like echo "<td class="myclass">{$position}</td>";
September 3, 200719 yr ... but with html you usually have double quotes in the code so it doesn't work like echo "<td class="myclass">{$position}</td>"; It does as long as you escape the quotes. echo "<td class=\"myclass\">{$position}</td>"; Like this.
September 3, 200719 yr Author Thanks for breaking everything down, thats easier to remember and to understand!
Create an account or sign in to comment