?
Solved

What does $return mean in this code?

Posted on 2016-09-19
6
Medium Priority
?
84 Views
Last Modified: 2016-09-28
Here's the code:

 if($number != false){
        foreach($messages as $message){
			echo "the number is good, so you're sending messages";
			echo "<br>";
          $req = $this->sendsms($number, $tp['phonenumber'], $message, $callback);
        }
      if($return == 'true'){
		  echo "you sent some texts";
        return $req;
      }else{
        echo json_encode($req);
      }
    } 

Open in new window


$this->sendsms is this code:

  public function sendsms($to, $from, $message, $callback = ''){
    $url = '/2010-04-01/Accounts/'. $this->AccountSID.'/SMS/Messages';
    $vars['To'] = $to;
    $vars['From'] = $from;
    $vars['Body'] = $message;
    $vars['StatusCallback'] = $callback;
    $req =  $this->twilio->request($url, 'POST', $vars);

    $req_array = json_decode(json_encode($req));
    //print_r($req_array); exit();
    //You know, just checking isError without returning actual error data, or syslogging it
    //is kind of ridiculous. I just wasted far too long trying to track down an invisible error.

    if($req_array->IsError === TRUE){
      //krumo($req_array);exit();

      //TODO :
      // If there's an error here, log it ya dingus. Visibility is good.
		echo "you tried, but you didn't send a text";
		echo "<br>";
      return false;
    }
    return $req;
  }

Open in new window


We're looping through a bunch of accounts. We get to a place where we've determined that the account is not bogus and the number is good, so we're now going to send an SMS. It looks as though "if($req_array->IsError === TRUE){" means that there's an error and the returned value will be false. Otherwise, we're getting the twilio request.

My question is: Does "  if($return == 'true'){" in the first code mean that sendsms fired correctly?
0
Comment
Question by:brucegust
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 35

Assisted Solution

by:Terry Woods
Terry Woods earned 332 total points
ID: 41805648
The value of $return isn't set in the given code. If it's set elsewhere (earlier) in your code, it may have some meaning, or it may just be a bug.

It's quite possible it should be this:
      if($req == 'true'){

Open in new window

0
 

Author Comment

by:brucegust
ID: 41805682
Terry!

I found it! You were right! It was defined earlier in the code. As a matter of fact, it was right in front of me on the first line:

 public function exportsms($accountid, $templateid, $statementid, $exportid, $return = 'true'){
	  echo "exportsms is happening";
	  echo "<br>";
    $messages = array();
    $messages[] = $this->sms->createsmsbody($accountid, $templateid, true, $statementid);
    //krumo(strlen($message));
    if (strlen($messages[0]) > 160){
      $messages[0] = str_replace("Text STOP to opt out", "", $messages[0]);
      $messages[1] = "Text STOP to opt out";
      //$error = Syslogs::entry('Message exceeds 160 characters. Accountid: '.$accountid, 'twiliov2/exportsms', 'ERROR', 'ERROR', "statementid:$statementid");
      //return false;
    }
    $callback = base_url().'twiliov2/smsbulkcomplete/'.$statementid.'/'.$exportid;
    $tp = Practices::getTwilioPhone($accountid);
    $number = $this->testaccountnumbers($accountid);
    $accountactive = Accounts::checkStatementWorthy($accountid);
    if($accountactive == false){
	echo "account is not active";
	echo "<br>";
      $error = Syslogs::entry('Client not Active or Account Balance < 0 :: AccountID ('.$accountid.') .', 'twiliov2/exportsms', 'ERROR', 'ERROR', "statementid:$statementid");
		echo "statement is not worthy";
      return false;
    }
    if($number != false){
        foreach($messages as $message){
			echo "the number is good, so you're sending messages";
			echo "<br>";
          $req = $this->sendsms($number, $tp['phonenumber'], $message, $callback);
        }
		var_dump($return);
      if($return == 'true'){
		  echo "you sent some texts";
        return $req;
      }else{
        echo json_encode($req);
      }
    } else {
		echo "something went south according to line 735 of twiliov2.php";
		echo "<br>";
      $error = Syslogs::entry('No cellular phone numbers found :: AccountID ('.$accountid.')', 'twiliov2/exportsms', 'ERROR', 'ERROR', "statementid:$statementid");
      return false;
    }
  }

Open in new window


But if it's defined on the very first line as "true" (public function exportsms($accountid, $templateid, $statementid, $exportid, $return = 'true')), what purpose does it serve? Can you tell?
0
 
LVL 83

Assisted Solution

by:Dave Baldwin
Dave Baldwin earned 332 total points
ID: 41805684
It may just be an error since 'return' is a key word in PHP and should not be used as a variable name.  http://php.net/manual/en/reserved.keywords.php
0
Are You Using the Best Web Development Editor?

The worlds of web hosting and web development are constantly evolving. Every year we see design trends change, coding standards adapt and new frameworks/CMS created. With such a quick pace of change it’s easy to get lost trying to keep up.

See if your editor made the list.

 
LVL 41

Assisted Solution

by:Sharath
Sharath earned 332 total points
ID: 41805687
In the function definition, $return has a default value as true. If you don't pass a value for this parameter, it is set to true by default.

public function exportsms($accountid, $templateid, $statementid, $exportid, $return = 'true')
0
 
LVL 58

Accepted Solution

by:
Julian Hansen earned 1004 total points
ID: 41806116
Following on from Sharath's comment - lets look at how it is used
if($return == 'true'){
  echo "you sent some texts";
  return $req;
} else {
  echo json_encode($req);
}

Open in new window

$return is a parameter that is passed to the function exportsms. If you don't expressly included it in the call to that function it defaults to true. In other words you only include the paramter (false) when you want to override the default behaviour.
Going back to the code in question

The default behaviour of the function is to
a) Output: "you sent some texts"
b) Return the $req value

If you send the $return value as false then the else fires and the output is a json_encoded string of the $req variable.

This is usually used when you want the same function to output to different consumers of the function.

In the default case the output is destined for the screen - so you get a friendly message and the $req value is returned in its native form.

In the second instance you might be returning the $req to another process (for instance an AJAX call) that is not interested in the friendly message and only wants the $req in a format it can understand - in this case JSON. So the JSON is output and the function does not return anything,
0
 
LVL 2

Expert Comment

by:Andrew Derse
ID: 41807695
The value for $return should be passed in when the function is called. If no value is passed, then it assigns the value as true.

So in breaking down the code, it's saying if $return is true, send me...if not, don't...

There must be other code out there evaluating something else, then calling this method to return or not return something.
0

Featured Post

7 Extremely Useful Linux Commands for Beginners

Just getting started with Linux? Here's a quick start guide that has 7 commands that we believe will come in handy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
Suggested Courses

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question