Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    Sep 2011
    Location
    127.0.0.1
    Posts
    65
    Thanks
    18
    Thanked 0 Times in 0 Posts

    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
    Last edited by sonic656; 07-17-2012 at 10:03 AM.
    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).

  • #2
    New Coder
    Join Date
    May 2012
    Posts
    17
    Thanks
    0
    Thanked 3 Times in 3 Posts
    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 />";

    }


  • Users who have thanked phpdude for this post:

    sonic656 (07-17-2012)

  • #3
    New Coder
    Join Date
    Sep 2011
    Location
    127.0.0.1
    Posts
    65
    Thanks
    18
    Thanked 0 Times in 0 Posts
    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).

  • #4
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,215
    Thanks
    12
    Thanked 338 Times in 334 Posts
    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.
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •