Solved

What does $return mean in this code?

Posted on 2016-09-19
6
61 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
6 Comments
 
LVL 35

Assisted Solution

by:Terry Woods
Terry Woods earned 83 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 83 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 40

Assisted Solution

by:Sharath
Sharath earned 83 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 52

Accepted Solution

by:
Julian Hansen earned 251 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

867 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now