#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2017
    Posts
    232
    Rep Power
    1

    Question What Is The Difference Between A Php Statement And A Php Expression?


    Programmer Folks!

    I first started learning php in September 2015. In 2 wks I read the whole tizag tutorial after understanding it.
    PHP Tutorial - Syntax
    (Browse the chapters list on the leftside of the page).

    I was still "raw" in experience when I suddenly read that php 7 was out and when some article mentioned there is a slight modification in the syntax of php 7, I got put-off from learning php 5 from any tutorial. Annoyed why the same language getting different set of syntax and so I deliberately tried forgetting what I learnt so far from tizag (which doesn't even mention what version they're teaching, like most tutorials forget to mention that) and waited nearly 1.5yrs for a beginner level tutorial to come-out that would teach me php 7 from ground-up but no such tutorial has come-out even today as most php 7 tutorials don't teach you from the beginning of the language but only teach you what is different in php 7 compared to php 5.
    Anyway, since, programmers everywhere keep telling me it;s the same language and not to wait for php 7 tutorial to come-out that would teach from the beginning and since they have assured me the syntax is still the same in php 7 like it was in php 5 and earlier versions then I have no choice but to revise again after 1.5yrs of my long waiting break.
    This time, however, going slowly in the "re-learning" so to get a solid understanding of the language and not overlook any aspect of it. Won't be run the learning this time. Else, I'll be hobbling throughout my php career. Saying all this, I do have a little experience in the language by watching youtube tutorials and fiddling with their codes modifying according to my needs and asking for help in the forums.
    Coming to the point:
    When I learnt from tizag, I can't remember them teaching anything about expressions or tokens (unless I've forgotten it over 1.5yrs) but going through tutorialspoint.com I came across it. Hence, the questions:

    Q1a. In php, what is the difference between a statement and an expression ?
    I know statements must end with a semi colon but what really is an expression and what really is a token ? I didnt really understand this explanation:
    https://www.tutorialspoint.com/php/p...x_overview.htm

    Guessing, a token is considered a data type (string, variable, integer, boolean, etc.) ?

    Q1b. Anything else I should know ?
  2. #2
  3. Lazy Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    16,260
    Rep Power
    9645
    1. Basically (so this is the short version that is technically not 100% accurate but is close enough),

    A statement is a line of code that gets executed.
    PHP Code:
    $foo "bar";
    echo 
    "baz";
    foreach (
    $array as $value) { }
    function 
    does_something() { } 
    Those are all statements. The first is an assignment, the second is... well, doesn't really have a name but it's generally called echoing/printing something, the third is a foreach loop, and the fourth is a function definition.

    An expression is something that gets evaluated and has a value. Most statements contain one or more expressions (the others are things like loops or function/class definitions).
    PHP Code:
    $foo "bar"
    "baz"
    function() { } 
    Those are expressions. The first is an assignment* (value** is "bar"), the second is a string (value is "baz"), and the third is an anonymous function (value is the function itself).

    Note that expressions and statements overlap a bit.

    A token is something else entirely. When PHP parses a file it breaks everything down into tokens, then assembles those tokens into operations, then performs those operations. For example,
    PHP Code:
    $foo "bar"
    has four tokens: a variable named "foo", an assignment operator, a string literal, and a semicolon. PHP recognizes the first three as an assignment expression, with the semicolon making the whole thing a statement, and when it executes it performs the "evaluate the thing on the right and store it into the variable named on the left" operation.

    2. Of course, but there's no way to list everything you could possibly need to know. Just keep learning.


    * This isn't actually something very well known but PHP's assignments are expressions, so you can use them in other places that expect expressions. This is mostly just a trivia fact though.
    PHP Code:
    echo ($foo "bar"); // outputs "bar"
    $one $two $three "four"// equivalent to $one = ($two = ($three = "four")) -> all three variables == "four" 
    ** Speaking of trivia, the value of an assignment expression is whatever was on the right. Not the thing on the left. And it's only evaluated once.

    Comments on this post

    • Will-O-The-Wisp agrees : Thanks!
    Last edited by requinix; April 18th, 2017 at 09:29 AM.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jul 2003
    Posts
    4,308
    Rep Power
    630
    Expressions only contain identifiers, literals and operators, where operators include arithmetic and boolean operators. The result of an expression is some value.
    PHP Code:
    $y+5
    A statement is everything that makes up a line or several lines of code. A statement can also be an expression or it can contain an expression.
    PHP Code:
    if ($l) {
       echo 
    "true"
    }
    else {
       echo 
    "false";

    Comments on this post

    • Will-O-The-Wisp agrees : Thank you!
    Last edited by gw1500se; April 18th, 2017 at 09:32 AM.
    There are 10 kinds of people in the world. Those that understand binary and those that don't.

IMN logo majestic logo threadwatch logo seochat tools logo