CLI PHP Exec New Page Not Returning Array

Discussion in 'PHP' started by lektrikpuke, Sep 27, 2012.

  1. #1
    I am running one page via CLI exec'ing another page. I'm trying to return values to the first page, but I cannot make an array return, only string. Is this something that cannot be done?

    Calling page:
    
    $valReturned = exec('invoked.php');
    print_r($valReturned);
    var_dump($valReturned);
    
    Code (markup):
    Called page that should return values to calling page:
    
    $txt = "Something that will return";
    $arr = array("one", "two", "three");
    
    echo $txt;
    $dispArr = ' ';
    foreach ($arr as $value){
    	$dispArr .= $value . "~";	
    }
    $trmdspArr = rtrim($dispArr,"~");
    echo $trmdspArr;
    return $txt . $trmdspArr . $arr;  // error msg: Array to string conversion
    
    Code (markup):

     
    Solved! View solution.
    lektrikpuke, Sep 27, 2012 IP
  2. #2
    exec will only return the last line of your output.

    Change the return in your invoked script to echo instead.
     
    ThePHPMaster, Sep 27, 2012 IP
  3. lektrikpuke

    lektrikpuke Well-Known Member

    Messages:
    297
    Likes Received:
    1
    Best Answers:
    1
    Trophy Points:
    113
    #3
    I was hoping that wasn't the answer. No matter what I tried (including return), nothing came back but a string.
     
    lektrikpuke, Sep 28, 2012 IP
  4. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,999
    Best Answers:
    253
    Trophy Points:
    515
    #4
    This might be a stupid question but... why the devil are you calling that with EXEC instead of just, I dunno... including it? Much less that 'return' only works inside FUNCTIONS, and if you are worried about namespace conflicts (the ONLY reason I can think to be using exec) put it IN a function.

    I don't think you're grasping what EXEC is and/or what it's for. You seem to have it confused with FUNCTION and/or INCLUDE... as well as brute-forcing string addition with FOREACH and RTRIM to do IMPLODE's job, etc, etc...

    What the devil are you even trying to do there?!?
     
    deathshadow, Sep 29, 2012 IP
  5. lektrikpuke

    lektrikpuke Well-Known Member

    Messages:
    297
    Likes Received:
    1
    Best Answers:
    1
    Trophy Points:
    113
    #5
    I'm using exec because my boss wants the calling page to be separate from the called page. I cannot use an include, nor can I use post or get. Not my choice. I used the bruteforce method for s&g. And the return was in addition to the echoes and was just me grasping at straws hoping something, anything would work.
     
    lektrikpuke, Sep 29, 2012 IP
  6. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,999
    Best Answers:
    253
    Trophy Points:
    515
    #6
    That makes no sense, at all... there is no fathomable or legitimate reason for that... at all.

    I'd say your boss is a dumbass who doesn't know enough on the topic to be telling you how to do something. Though admittedly the last "boss" I had I ended up screaming at "You think you can do a better job, you ******* do it! Otherwise, what the **** did you hire me for?!?" -- so I'm a bit biased in that department. I now have a zero tolerance policy for dumbasses who don't know jack telling other people how to code.

    Feel free to point that 'boss' at this thread -- I'd love to hear what lame dumbass idiotic noodle-doodle excuse they have for even trying to tell you to do it that way.
     
    deathshadow, Sep 29, 2012 IP
  7. lektrikpuke

    lektrikpuke Well-Known Member

    Messages:
    297
    Likes Received:
    1
    Best Answers:
    1
    Trophy Points:
    113
    #7
    Actually, he has a good reason, which is off topic from my original post. I do appreciate the passion as I've been there. However, my new motto is to do no harm.
     
    lektrikpuke, Sep 29, 2012 IP
  8. ThePHPMaster

    ThePHPMaster Well-Known Member

    Messages:
    737
    Likes Received:
    52
    Best Answers:
    33
    Trophy Points:
    150
    #8
    I probably did not read the question correctly. I see what you want to do now. Here is what I would do in this case:

    In the executed thread:
    
    $returns = array (
    'txt' => $txt,
    'trmdspArr' => $trmdspArr,
    'arr' => $arr;
    );
    echo json_encode($returns);
    
    PHP:
    and in your main script, you will decode it to get an object or array:

    
    $valReturned = exec('invoked.php');
    $valReturned = json_decode($valReturned);
    
    PHP:
     
    ThePHPMaster, Sep 29, 2012 IP
  9. lektrikpuke

    lektrikpuke Well-Known Member

    Messages:
    297
    Likes Received:
    1
    Best Answers:
    1
    Trophy Points:
    113
    #9
    Believe it or not, we are using json_encode/decode (the boss wants that, too) for part of our information passing, just not at that point. Maybe we should. Thanks.
     
    lektrikpuke, Sep 30, 2012 IP
  10. lektrikpuke

    lektrikpuke Well-Known Member

    Messages:
    297
    Likes Received:
    1
    Best Answers:
    1
    Trophy Points:
    113
    #10
    And, I will talk to my boss about using an include (tomorrow). Perhaps I misunderstood his requirements for this project. Either way, thanks guys!
     
    lektrikpuke, Sep 30, 2012 IP