Jump to content

[SOLVED] very simple echo and variable questions


noidtluom

Recommended Posts

Since I am trying to improve my code optimization, I have some simple questions.

 

Is there any difference between echo (""); and echo ""; and which one is better to use?

 

Should I use

echo "$var is a very cool variable and so is $anothervar";

or

echo $var ." is a very cool variable and so is ". $anothervar;

or

echo $var ," is a very cool variable and so is ", $anothervar;

 

and if possible, why?

 

Also, is there any suggestions for naming variables? I normally just name them $whatever $i $want. (of course, if the variable contains a username I name it something like $username.) So is there any sort of format that should be used? I think I read somewhere that they should be (of course, not mandatory) named like this: $twoWords or $anotherVariable, where the second word's first letter is capitalized.

 

Thanks in advance.

Link to comment
Share on other sites

That is called camelCase. It's how I prefer to name, but there are other types, if you lookup naming conventions.

I would actually do this:

echo $var .' is a very cool variable and so is '. $anothervar;

With single quotes. If you use double, PHP has to parse the string for variables like in your first example. It's "slightly" faster, but I prefer it because it is easier for me to read. If you use a color-coded IDE you'll be able to see your strings and your variables separately, it helps in debugging.

Link to comment
Share on other sites

Thank you.

 

With single quotes. If you use double, PHP has to parse the string for variables like in your first example. It's "slightly" faster, but I prefer it because it is easier for me to read.

 

I'm sorry I didn't really understand your sentence. Which one is faster, the " or ' ? and which one do you think is easier to read? I got confused with the use of "it" in your sentences.

 

Have you also got an answer for the difference between echo (""); and echo ""; ? and which should be used?

 

Thanks in advance.

Link to comment
Share on other sites

Single quotes are faster than double quotes, because single quoted strings don't need to be examined by php.  But double quotes strings may contain variables, so php has to check for that first.

 

My syntax highlighter (vim) highlights variables inside double quotes.

 

Regarding echo, it doesn't matter.  Even if there is a speed difference there, it'll be too small to matter.  Both act the same way.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.