Solved

What does $return mean in this code?

Posted on 2016-09-19
6
53 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 82

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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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 51

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 1

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

707 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

17 Experts available now in Live!

Get 1:1 Help Now