stevenpepe
stevenpepe

Reputation: 267

Testing for array PHP

I created an array below:

$email[] = $post[0];

$email[].= $post[2];

The result is:

$email = Array ( [JESSICA] => [email protected] ) 

I then pass it to a class constructor as so:

$email_user = new Email($id,$email,$subject,$heading,$messages,$keys);

The class looks like this:

class Email extends Users { 

protected $id;
public $strings;
public $user_email;
public $subject;
public $heading;
public $messages;
public $keys;

public function __construct($id,$user_email,$subject,$heading,$messages,$keys) {

parent::__construct($id);
$this->user_email = $user_email;
$this->subject = $subject;
$this->heading = $heading;
$this->messages = $messages;
$this->keys = $keys;

If I test to see if $this->user_email is an array using:

if(is_array($this->user_email)) {
   echo "TRUE";
}

it returns false. How come?

* I found the issue, a conflict with variables both named $email. Thanks for the help.

Upvotes: 1

Views: 89

Answers (2)

Kristian
Kristian

Reputation: 21830

Beware that the use of the .= operator is generally used for concatinating two strings.

by saying $email[] = somevalue, you're essentially pushing the value arbitrarily into the array, which is totally good enough for what you're doing. You end up with:

$email[] = $post[0];
$email[] = $post[2];

Upvotes: 2

Adam
Adam

Reputation: 1694

try:

public $user_email = array();

Remove the concatenation: $email[].= $post[2]; $email[] = $post[2]; is fine.

Upvotes: 0

Related Questions