...

View Full Version : How to do this for() function in PHP?



sonic656
07-17-2012, 11:00 AM
var names = ["Chris", "Kate", "Steve"]; //some values
for(var i in names) {alert(names[i]);}
Hi. This is script that I use in JavaScript. I need to get it working in PHP.
This is not working:


//$_GET['emails']="test@test.com, blablabla@bla.com"; //some values

$Emails = explode(",",strip_tags($_GET['emails']));
for($i in $Emails) {
if(smail($subject, $message, "no-reply@website.com", $Emails[$i])) {echo "sent to:".$Emails[$i]."<br />";}
}

error:


Parse error: syntax error, unexpected T_STRING, expecting ';' in localhost/www/send.php on line 33
How to get it woking?

PS: Function of this script in JavaScript: It will alert one name by another from array.
PS 2: sry for my english :D

phpdude
07-17-2012, 11:39 AM
should work


//$_GET['emails']="test@test.com, blablabla@bla.com"; //some values

$emails = explode(",", str_replace(" ", "", $_GET['emails']));
foreach($emails as $email)

{

if(mail($email, $subject, $message) )

{

echo "sent to:".$email."<br />";

}
}

sonic656
07-17-2012, 04:19 PM
good idea phpdude...let's try it! thx.

Dormilich
07-18-2012, 11:28 AM
var names = ["Chris", "Kate", "Steve"]; //some values
for(var i in names) {alert(names[i]);}
Hi. This is script that I use in JavaScript. I need to get it working in PHP.

I might note that the for...in loop in JavaScript is used to loop over the enumerable properties of an object. for looping over an array either use a standard for() loop or the array’s forEach() method.

example where for...in and for() have a different behaviour:

var names = ["Chris", "Kate", "Steve"];
names["John"] = "Doe"; // looks like an associative array? it does, but an associative array does not exist in JS, you’re just extending your array object

var count = 0;
for (var i = names.length; i--;) {
count++;
}
alert(count); // 3
count = 0;
for (var j in names) {
count++;
}
alert(count); // 4

in case of PHP, the standard array is something completely different to a JavaScript array (the closest would be the SplFixedArray object), but yes, foreach() would be the structure to go. esp. since foreach() (PHP) is almost equivalent to JavaScripts for...in and for each...in. PHP’s foreach() is also able to loop over object properties and Iterator objects.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum