Jump to content

Basic Syntax Questions


Mark H

Recommended Posts

I use Dreamweaver for my coding, and, by and large, the colour indications tell me when I've got the syntax completely wrong.However, that's only by and large. There are some areas I'm unclear on.So, for beginners like myself, I thought I'd start this thread with the really basic questions.1. Setting Variables.I know that the general tag for a variable is $. e.g. $userBut, when creating these variables or adjusting them, do we use parantheses after the operator?e.g.

$user = 'Mark'

or:

$user = ('Mark')

2. If and Else if StatementsI know that following an if, or an else, or an else if I can use curly brackets to enclose the block of code to execute if the conditions are met. But when there is only one line of code, what is the correct format?3. QuotesI have been using ' (single quotes) as my enclosing quotes for variables in conditions etc. But, as I looked at the tutorials, sometimes " (double quotes) is used.Are these basically identical, and only used if you've got nested quotes?Many thanks. Let me know if there are any other "stupid" questions I haven't come across. :)

Link to comment
Share on other sites

1. You may, but you don't have to. In this case, it won't make any difference. In other more complex variable assignments, you may use parantheses to clear up what happens after what, similarly to how you do it in math classes, e.g.

$workDaysInYear = 12 * (((7-2) * 4) + $delta)

2. It's the same. "A block of code" may include zero, one or more statements (or "lines of code" if you prefer). You could omit the curly braces when there's only one statement, but that's generally not considered a good practice, because if you later decide to add a second line, you'll have to remember to add the curly braces back, or you risk making the script behave in an unexpected way.3. In PHP, double quotes have some extra features, but unless you need them, using single quotes is a better decision. See the PHP manual on strings for details.

Link to comment
Share on other sites

3. In PHP, double quotes have some extra features, but unless you need them, using single quotes is a better decision. See the PHP manual on strings for details.
Stick with single quotes until you need double quotes as the string-syntax section that boen-robot has pointed out indicates. Single quotes are about as close as you can come to the meaning of double quotes in normal writing, and you can enter them without the use of the shift key.Roddy
Link to comment
Share on other sites

Hah. I'm a novelist as well as a college professor (no, I'm not famous). I find it very difficult NOT to hit the shift key when my brain is thinking "quotes." I have to think very hard to remind myself to use single quotes. Often I catch it when I'm editing.

Link to comment
Share on other sites

I keep my quote style usage specific to the languages I use. For example, I almost always use doubles in PHP and singles in JS and HTML. That way when I'm writing out my HTML in a PHP echo statement I don't have to keep track of which quotes I'm using and whether they need to be escaped.

Link to comment
Share on other sites

Yes. A lot of escaping makes code hard to read. I avoid it when I can. Always using doubles in PHP gives you the option of interpolating a variable or special character if you need to update the code. Less stuff to revise.And we all know the speed benchmark differences are trivial, right?Using quotes "correctly" in PHP is mostly a matter of showing experienced developers that you're a member of the club.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...