ParsX.com
پذیرش پروژه از دانشجویی ... تا سازمانی 09376225339
 
   ProfileProfile   Log in to check your private messagesLog in to check your private messages  |  FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups Log inLog in   RegisterRegister 

آرايه انجمني $GLOBALS و دستور foreach

 
Post new topic   Reply to topic    ParsX.com Forum Index -> Writers
View previous topic :: View next topic  
Author Message
vahid
بي تو هرگز


Joined: 26 Nov 2004
Posts: 3067
Location: Tehran

PostPosted: Wed Jul 11, 2007 12:38 pm    Post subject: آرايه انجمني $GLOBALS و دستور foreach Reply with quote

يكي از روش هاي دستيابي به متغيرهاي عمومي استفاده از آرايه انجمني $GLOBALS مي باشد . براي دسترسي به متغيري عمومي به نام $parsx در يك تابع مي توان با بازخواني $GLOBALS[parsx] به آن دسترسي پيدا كرد .
يكي از روشهاي پيمايش آرايه استفاده از دستور foreach ‌ مي باشد كه در PHP4 پياده سازي شده است .
foreach($array as $temp ) {
//statements
}

كه در آن $array آرايه و $temp يك متغير موقتي است كه مقدار هر المان آرايه را در خود نگه مي دارد .
مثلا در مثال زير :
$users = array( "arash","amir","vahid");
foreach($users as $val)
{
Print "$val<br>";
}

اما براي دسترسي به انديس آرايه بايد دستور Foreach ‌را كمي تغيير دهيد .
foreach($array as $key => $value )
{
//…
}

مثلا :
$character = array (    Name=>"parsx",
         Occupation=> ".com",
         Age=>30,
         "special powe"=>"x-ray vision ");
foreach($character as $key=>$val){
   Print "$key = $val";
}

مثالي براي نمايش متغيرهاي عمومي :
$value1="parsx";
$value2="vahid";
$value3="mohammad";

foreach($GLOBALS as $key =>$val){
   Print " \$GLOBALS[\"$key\"]==$val<br>";}
Back to top
Display posts from previous:   
Post new topic   Reply to topic    ParsX.com Forum Index -> Writers All times are GMT + 3.5 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum