如何让PHP命名这个div?

So I want php to name the div whatever the value of B is. Example: if b = 1, I want the div id to be "imgDiv-1". The code generates 3 divs, and each div has to have a different name. However, this code makes every div id to be "imgDiv-3" Code:

<?php
for ($b=3; $b > 4; $b++) {
    define('RANDOM_IMAGES_COUNT2',3);
    define('RANDOM_IMAGES_FORMAT2', '<div id="imgDiv-' .$b. '"style="width:170px; height:auto;float:left;text-align:center;top"><img src="%s" style="border-style:solid;border-width:2px;border-color:black;"/><a href="%s" alt="%s" title2="%s">%s</a></div>');
}

#------------------------------------------------------------------------------

$images = array (
    array ( 'title2' => 'Test 2', 'src2' => 'pic2.jpg', 'href2' => 'http://mylink.com  /path/','text2' => 'Hello' ),
    array ( 'title2' => 'Test 2', 'src2' => 'pic7.jpg', 'href2' => 'http://mylink.com/path/','text2' => 'Hello2' ),
    array ( 'title2' => 'Test 2', 'src2' => 'pic9.jpg', 'href2' => 'http://mylink.com/path/','text2' => 'Hello2' ), 
    array ( 'title2' => 'Test 2', 'src2' => 'pic5.jpg', 'href2' => 'http://mylink.com/path/','text2' => 'Hello2' ),     
    array ( 'title2' => 'Test 2', 'src2' => 'pic3.jpg', 'href2' => 'http://mylink.com/path/','text2' => 'Hello3' )
);

#------------------------------------------------------------------------------

if ( count($images) < RANDOM_IMAGES_COUNT2 ) {
    trigger_error('Not enough images given', E_USER_WARNING);
    exit;
}

#------------------------------------------------------------------------------

for ($i = 0; $i < RANDOM_IMAGES_COUNT2; $i++) {
    shuffle($images);

    $tmp = array_shift($images);
    printf( RANDOM_IMAGES_FORMAT2, $tmp['src2'], $tmp['href2'], $tmp['title2'],     $tmp['title2'],$tmp['text2'] );    }
?>

Your code has multiple issues.

Firstly, your loop never runs. You initalize $b to 3 and have it run while $b > 4, which is never the case. You probably wanted this: for ($b=1; $b < 4; $b++)

Secondly, you need to understand what define() does. It defines a constant, which cannot be changed. So setting it within a loop is a no-no.

Perhaps this is what you wanted?

define('RANDOM_IMAGES_COUNT', 3);
define('RANDOM_IMAGES_FORMAT', '
    <div id="imgDiv-%s" style="width:170px; height:auto; float:left; text-align:center;">
        <img src="%s" style="border-style:solid; border-width:2px; border-color:black;" />
        <a href="%s" alt="%s" title2="%s">%s</a>
    </div>
');

$images = array (
    array ( 'title2' => 'Test 2', 'src2' => 'pic2.jpg', 'href2' => 'http://mylink.com  /path/','text2' => 'Hello' ),
    array ( 'title2' => 'Test 2', 'src2' => 'pic7.jpg', 'href2' => 'http://mylink.com/path/','text2' => 'Hello2' ),
    array ( 'title2' => 'Test 2', 'src2' => 'pic9.jpg', 'href2' => 'http://mylink.com/path/','text2' => 'Hello2' ), 
    array ( 'title2' => 'Test 2', 'src2' => 'pic5.jpg', 'href2' => 'http://mylink.com/path/','text2' => 'Hello2' ),     
    array ( 'title2' => 'Test 2', 'src2' => 'pic3.jpg', 'href2' => 'http://mylink.com/path/','text2' => 'Hello3' )
);
if (count($images) < RANDOM_IMAGES_COUNT) {
    trigger_error('Not enough images given', E_USER_WARNING);
    exit;
}
shuffle($images);
for ($i=1; $i<=RANDOM_IMAGES_COUNT; $i++) {
    printf(
        RANDOM_IMAGES_FORMAT,
        $i,
        $images[$i]['src2'],
        $images[$i]['href2'],
        $images[$i]['title2'],
        $images[$i]['title2'],
        $images[$i]['text2']
    );
}

Since you have $b=3 to start with and the loop can only run while $b is less than 4 (so when $b is 3...which it is) then your loop is pointless. $b can never be equal to 1.

Perhaps you want to do this: for ($b=1; $b < 4; $b++) {.

This means that $b will be 1 at first, then run the code again as 2, then 3, and then will will be done.

so all in all, from all the comments you should probably change the for loop to match

for($b = 1; $b < 4; b++)
{
}

this will make

imgDiv-1, imgDiv-2, imgDiv-3

also if you have any question using DEFINE()

http://php.net/manual/en/function.define.php

is a good resource

Change in for loop

Write

for ($b=1; $b < 4; $b++) {
        define('RANDOM_IMAGES_COUNT2',3);
        define('RANDOM_IMAGES_FORMAT2', '<div id="imgDiv-' .$b. '"style="width:170px; height:auto;float:left;text-align:center;top"><img src="%s" style="border-style:solid;border-width:2px;border-color:black;"/><a href="%s" alt="%s" title2="%s">%s</a></div>');
    }

Instead of

for ($b=3; $b > 4; $b++) {
    define('RANDOM_IMAGES_COUNT2',3);
    define('RANDOM_IMAGES_FORMAT2', '<div id="imgDiv-' .$b. '"style="width:170px; height:auto;float:left;text-align:center;top"><img src="%s" style="border-style:solid;border-width:2px;border-color:black;"/><a href="%s" alt="%s" title2="%s">%s</a></div>');
}