Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 07-17-2012, 10:00 AM   PM User | #1
sonic656
New Coder

 
Join Date: Sep 2011
Location: 127.0.0.1
Posts: 62
Thanks: 18
Thanked 0 Times in 0 Posts
sonic656 is an unknown quantity at this point
How to do this for() function in PHP?

Code:
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:
PHP Code:
//$_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:
Code:
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
__________________
My website is here: http://www.moowdesign.eu/. It is not complete yet, and I want to add language translations for some languages(including english).

Last edited by sonic656; 07-17-2012 at 10:03 AM..
sonic656 is offline   Reply With Quote
Old 07-17-2012, 10:39 AM   PM User | #2
phpdude
New Coder

 
Join Date: May 2012
Posts: 17
Thanks: 0
Thanked 3 Times in 3 Posts
phpdude is an unknown quantity at this point
should work

PHP Code:
//$_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 />";

}

phpdude is offline   Reply With Quote
Users who have thanked phpdude for this post:
sonic656 (07-17-2012)
Old 07-17-2012, 03:19 PM   PM User | #3
sonic656
New Coder

 
Join Date: Sep 2011
Location: 127.0.0.1
Posts: 62
Thanks: 18
Thanked 0 Times in 0 Posts
sonic656 is an unknown quantity at this point
good idea phpdude...let's try it! thx.
__________________
My website is here: http://www.moowdesign.eu/. It is not complete yet, and I want to add language translations for some languages(including english).
sonic656 is offline   Reply With Quote
Old 07-18-2012, 10:28 AM   PM User | #4
Dormilich
Senior Coder

 
Dormilich's Avatar
 
Join Date: Jan 2010
Location: Behind the Wall
Posts: 2,855
Thanks: 9
Thanked 288 Times in 284 Posts
Dormilich is on a distinguished road
Quote:
Originally Posted by sonic656 View Post
Code:
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:
PHP Code:
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 
names.lengthi--;) {
    
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.
__________________
please post your code wrapped in [CODE] [/CODE] tags
Dormilich is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 05:06 AM.


Advertisement
Log in to turn off these ads.