0

i have the following functions

function a ($var) {
somecode;
b($var);

}

function b ($var) {
c($var);
}

function c($var) {

}

then suppose that i call these functions in a script

a(1);
a(2);

in programming languages like java for example, the normal order of execution is a(1) is executed then b(1) then c(1) then a(2) then b(2) then c(2).it means that a(2) is not called until all functions called by a(1) finish execution which inturn call other functions. but in php it didnt follow like that.in php it may go like that: a(1) then b(1) then a(2) then c(1). how to make sure that a(2) is not called until a(1) and all the functions that a(1) may call finish executing ? by the way i am calling the functions in cakephp with autorender=false, is cakephp responsible for that ?

6
  • try debug your code ! function a ($var) { echo 'function a with : ' , $var , '<br />'; b($var); } function b ($var) { echo 'function b with : ' , $var , '<br />'; } Commented Feb 18, 2014 at 12:03
  • It should work exactly like you want it too, because PHP is run from top to bottom. It's more likely to be an issue in your code. Commented Feb 18, 2014 at 12:05
  • well maybe "somecode" include async function like ajax? If so, then you need to use callback function. Commented Feb 18, 2014 at 12:06
  • @GolezTrol - Wrong use of word. What I was aiming for was what you have in your answer. It's been a stressy day! My apologies. Commented Feb 18, 2014 at 12:07
  • i debuged the code , it is in cakephp, it didnt go in the desired order Commented Feb 18, 2014 at 12:08

2 Answers 2

1

PHP runs sequential, like the other examples you gave. There is no different order, and nothing special you need to do to make this work.

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

5 Comments

inside code is insert queries, so when i debuged , i noticed the problem
do you call ajax there to fetch data?
i mean to insert data :)
I think your test is wrong. If you have code like that, it should run sequentially. Only possible exception, is when you start forking/threading, but that is advanced territory, especially in PHP. If that happens, it happens in 'somecode', but there is no way for me to tell what happens in code you didn't post.
Anyway, 'normal' PHP code, including database queries and such runs sequentially. No matter if you call it 1, 50 or 1000 times, those echoes will always appear in the same order.
0

I don't think so..it runs in sequential order. I got little confused so I checked myself(to calm down my heartbeat):

<?php
function a ($var) {
  echo 'a->'.$var.'<br>';
  b($var);
}

function b ($var) {
  echo 'b->'.$var.'<br>';
}

a(1);
a(2);
?>

Gives you:

a->1
b->1
a->2
b->2

phew!

7 Comments

you may get that right when there are only 2 functions.but when there are about 10 functions with hundred of functions call, the difference will be obvious
only async functions dont run in sequental order.
may be the code inside somecode is creating this type of behaviour. Even if a file has a hundred functions, it will be executed sequentially until unless there is some call to other function in between.
then, how to make sure that functions are not async ?
Did you use ajax? Or a multithread application that had access to your website?
|

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.