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?
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.
Thanks for confirmation !
I really didn’t want to have to go back and mod the php code I had just written.
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.
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.
If you wanted to make sure it completed OK first, you could do something like this:
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.
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.
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.