This is my first php program!



Download 6,9 Mb.
Pdf ko'rish
bet25/69
Sana26.02.2022
Hajmi6,9 Mb.
#471647
1   ...   21   22   23   24   25   26   27   28   ...   69
Bog'liq
PHP

32
|
Chapter 2: Language Basics
$counter = 10;
update_counter( );
echo $counter;
10
The
$counter
inside the function is local to that function, because we haven’t said
otherwise. The function increments its private
$counter
, whose value is thrown away
when the subroutine ends. The global
$counter
remains set at 10.
Only functions can provide local scope. Unlike in other languages, in PHP you can’t
create a variable whose scope is a loop, conditional branch, or other type of block.
Global scope
Variables declared outside a function are global. That is, they can be accessed from
any part of the program. However, by default, they are not available inside func-
tions. To allow a function to access a global variable, you can use the
global
key-
word inside the function to declare the variable within the function. Here’s how we
can rewrite the
update_counter( )
function to allow it to access the global
$counter
variable:
function update_counter ( ) {
global $counter;
$counter++;
}
$counter = 10;
update_counter( );
echo $counter;
11
A more cumbersome way to update the global variable is to use PHP’s
$GLOBALS
array
instead of accessing the variable directly:
function update_counter ( ) {
$GLOBALS[counter]++;
}
$counter = 10;
update_counter( );
echo $counter;
11
Static variables
A static variable retains its value between calls to a function but is visible only within
that function. You declare a variable static with the
static
keyword. For example:
function update_counter ( ) {
static $counter = 0;
$counter++;
echo "Static counter is now $counter\n";
}
$counter = 10;
,ch02.15294 Page 32 Wednesday, March 13, 2002 11:42 AM


This is the Title of the Book, eMatter Edition
Copyright © 2002 O’Reilly & Associates, Inc. All rights reserved.

Download 6,9 Mb.

Do'stlaringiz bilan baham:
1   ...   21   22   23   24   25   26   27   28   ...   69




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©hozir.org 2024
ma'muriyatiga murojaat qiling

kiriting | ro'yxatdan o'tish
    Bosh sahifa
юртда тантана
Боғда битган
Бугун юртда
Эшитганлар жилманглар
Эшитмадим деманглар
битган бодомлар
Yangiariq tumani
qitish marakazi
Raqamli texnologiyalar
ilishida muhokamadan
tasdiqqa tavsiya
tavsiya etilgan
iqtisodiyot kafedrasi
steiermarkischen landesregierung
asarlaringizni yuboring
o'zingizning asarlaringizni
Iltimos faqat
faqat o'zingizning
steierm rkischen
landesregierung fachabteilung
rkischen landesregierung
hamshira loyihasi
loyihasi mavsum
faolyatining oqibatlari
asosiy adabiyotlar
fakulteti ahborot
ahborot havfsizligi
havfsizligi kafedrasi
fanidan bo’yicha
fakulteti iqtisodiyot
boshqaruv fakulteti
chiqarishda boshqaruv
ishlab chiqarishda
iqtisodiyot fakultet
multiservis tarmoqlari
fanidan asosiy
Uzbek fanidan
mavzulari potok
asosidagi multiservis
'aliyyil a'ziym
billahil 'aliyyil
illaa billahil
quvvata illaa
falah' deganida
Kompyuter savodxonligi
bo’yicha mustaqil
'alal falah'
Hayya 'alal
'alas soloh
Hayya 'alas
mavsum boyicha


yuklab olish