SitePoint Sponsor

User Tag List

Results 1 to 10 of 10

Hybrid View

  1. #1
    SitePoint Enthusiast
    Join Date
    Feb 2011
    Posts
    48
    Mentioned
    0 Post(s)
    Tagged
    1 Thread(s)

    php an enforced sequential language?

    I'm a front end person who is doing more and more php.
    Also I'm used to javascript where I use a callback to make sure a function has completed before using the results of that function.

    Recently I was in a situation with PHP where line 10 uses the results of a function and assigns them to a variable.
    Line 11 then uses that variable, there is no check to see if the function has completed.
    My instinct was to change it to include a callback.
    But someone told me that php is a sequential language and that every line is evaluated before moving to the next. Is this true?

  2. #2
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    PHP is sequential, it runs functions in order they are declared. That is not to say you cannot use callbacks. Javascript is also sequential along with almost every programming language. They run in the order you define the steps. Otherwise, assigning the result of a function to a variable then using that variable would break. But that does not happen, because the function is not ran with in an async mode.
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  3. #3
    SitePoint Enthusiast
    Join Date
    Feb 2011
    Posts
    48
    Mentioned
    0 Post(s)
    Tagged
    1 Thread(s)
    Thanks for confirmation !
    I really didn't want to have to go back and mod the php code I had just written.
    Quote Originally Posted by logic_earth View Post
    Javascript is also sequential ...
    yes this is true and I realized after I had posted.
    Because I work so closely with the DOM and continually loading containers I am in a habit of not trusting anything and relying on callbacks before proceeding because quite often something hasn't loaded, code is missing, variable haven't been assigned and yet we've moved on.
    Thinking that way meant I could program faster with less bug fixing. I hope that explains my mindset and why I often treat javascript and jQuery as if they were non-sequential.

  4. #4
    SitePoint Addict kduv's Avatar
    Join Date
    May 2012
    Location
    Maui, HI
    Posts
    211
    Mentioned
    5 Post(s)
    Tagged
    0 Thread(s)
    Make sure you keep in mind variable scope. Variables set inside a function are not accessible outside that function unless it is a global variable.

    For instance, this will not work:
    PHP Code:
    <?php
    function someFunction($str)
    {
        
    $var $str;
    }

    someFunction('some string');

    echo 
    $var// $var is undefined
    ?>
    But this will:
    PHP Code:
    <?php
    function someFunction($str)
    {
        return 
    $str;
    }

    $var someFunction('some string');

    echo 
    $var// echoes some string
    ?>

  5. #5
    SitePoint Enthusiast
    Join Date
    Feb 2011
    Posts
    48
    Mentioned
    0 Post(s)
    Tagged
    1 Thread(s)
    thanks kduv, I try to be careful of that

    for reference the code I was concerned about was

    line 10: $newdata = processData($data, $old_data);
    line 11: $mysqli->query("UPDATE assetsEn SET datastring='$newdata', commit=$commit WHERE IDdata=".$catch->IDdata);
    and in my javascript way of thinking I would use a callback to ensure processData() had completed and passed value before doing something important with it on line 11, but the php dev I have been working with said it was fine and thats how we started talking about sequential.

  6. #6
    SitePoint Addict kduv's Avatar
    Join Date
    May 2012
    Location
    Maui, HI
    Posts
    211
    Mentioned
    5 Post(s)
    Tagged
    0 Thread(s)
    If you wanted to make sure it completed OK first, you could do something like this:
    PHP Code:
    function processData($data$old_data)
    {
        
    // if something goes wrong
        
    return false;
      
        
    // if everything is good
        
    return 'whatever you want';
    }

    $newdata processData($data$old_data);
    if (
    $newdata !== false) {
        
    $mysqli->query("UPDATE assetsEn SET datastring='$newdata', commit=$commit WHERE IDdata=".$catch->IDdata);

    You could also use the various error/exception functions and classes included in PHP to either trigger an error or throw/catch an exception if something goes wrong in your function.

    You should know what your function "should" return. Therefore, if something happens unexpected in your function, you can handle/test for it accordingly.

  7. #7
    SitePoint Enthusiast
    Join Date
    Feb 2011
    Posts
    48
    Mentioned
    0 Post(s)
    Tagged
    1 Thread(s)
    Its more about timing than checking for errors (my error checking gets logged out to a .txt file)
    the processData function is incredibly involved and has some massive loops, many internal arrays and calls 2 or three sub functions.

    when I do stuff like this in javascript, line 11 nearly always gets called before line 10 has completed.

  8. #8
    SitePoint Enthusiast
    Join Date
    Feb 2011
    Posts
    48
    Mentioned
    0 Post(s)
    Tagged
    1 Thread(s)
    actually I just went though my code ... processData makes four queries to mysql and each of those queries might take a small amount of time
    I went to phpMyAdmin to look up a record and it said.
    Query took 0.0009 sec
    This would mean processData might take 0.0036 seconds to complete. Are you all sure that php will not progress to the next line?
    I'm just being paranoid because of my experiences with javascript, DOM and the mental workflow I have while coding. So far the php code runs fine without a hitch but its light loads we are testing with.

  9. #9
    SitePoint Addict kduv's Avatar
    Join Date
    May 2012
    Location
    Maui, HI
    Posts
    211
    Mentioned
    5 Post(s)
    Tagged
    0 Thread(s)
    Yeah, you'll be fine. PHP won't go on to the next line until the current line is finished.

  10. #10
    SitePoint Enthusiast
    Join Date
    Feb 2011
    Posts
    48
    Mentioned
    0 Post(s)
    Tagged
    1 Thread(s)
    thx kduv


Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •