1

Im new to PHP, and I am learning about control structures. I just learned about if statements, switch statement and while loops. I know the syntax for an if statement is:

if (condition)
{
    //code to be executed if the condition is true;
}

switch syntax:

switch (expression)
{
    case 1:
    //code to be executed;
    break;

    case 2:
    //code to be executed;
    break;

    default:
    //code to be executed;
}

and the syntax for a while loop is:

while (expression)
{
     //code to be executed if the expression is true;
}

I see the terms condition and argument and expression pretty interchangeably. Do they all mean the same thing? If not what are the differences?

0

5 Answers 5

2

The condition is an expression which will be evaluated as a boolean. If it evaluates as true, the code is executed, if it evaluates as false the code is skipped.

Arguments are just parameters for functions usually, I also don't see the term in your code samples.

Also be sure to end all cases in a switch statement with a break; If you don't the next case will also be executed and so on.

Sign up to request clarification or add additional context in comments.

Comments

2

Conditions, Arguments and Expressions are parts of 'speech' of the PHP Language. They are not interchangeable. Their difference mainly lies in where in a PHP sentence (aka a 'statement') they can be used, and are defined by the grammar of the PHP language.

Statements end in semicolons or are enclosed by curly braces ( { and } )

Expressions evaluate to a value, e.g. 1+2 is an expression, and so is $a = 1+2. $a = 1 + 2; is a statement made of a single expression. $a = $b = 1 + 2; is a statement made of two expressions.

Condition is another word for a boolean expression. A boolean expression is an expression that evaluates to either a value of true or a value of false.

Argument is the value passed in to a parameter of a function. People sometimes talk about the 'argument' to an if statement, but this is technically not correct. if/while/for/foreach take expressions. The fact that they look like a function call is just syntatic sugar.

Comments

0

I would use this terminology:

if (condition)
   body
while (condition)
   body
switch (subject) {
   cases
}

That said, some people will refer to the condition of a while loop as an argument, for example. In my opinion this isn't the best terminology, because an argument is something passed to a function, and if/while/switch are not functions in PHP (they are in some languages).

But you're right, some people will say "the argument of the while loop." Just understand that they mean "the condition of the while loop."

Comments

0

While is repeating cycle. It means "do all in brackets while my expresion"

if and switch are the same. The diference is just in readibility.

Comments

0

An expression is simply something that expresses something - in a if/switch/while, the expression determines which code path should be taken. Usually the expression boils down to a simple boolean true/false value, or something that can be converted into a boolean value.

However, you can also consider the expression to be an argument, if you pretend that if() and while() are function calls - the expression are the arguments for those pseudo-functions. But again, the arguments will get distilled down to simple boolean values.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.