FUDforum
Fast Uncompromising Discussions. FUDforum will get your users talking.

Home » Imported messages » comp.lang.php » scoping inside a block
Show: Today's Messages :: Polls :: Message Navigator
Switch to threaded view of this topic Create a new topic Submit Reply
scoping inside a block [message #176349] Tue, 27 December 2011 20:08 Go to next message
oldyork90 is currently offline  oldyork90
Messages: 9
Registered: December 2011
Karma: 0
Junior Member
Is there a way to localize a var inside a block? Can I have this
behavior without calling a function?


$myA = 'A'

{
$myC = 'C';
$myA = 'B';
echo $myA . ' says B';
}

echo $myA . ' says A';
echo $myC .... does not exist


Thank you.
Re: scoping inside a block [message #176354 is a reply to message #176349] Tue, 27 December 2011 21:06 Go to previous messageGo to next message
The Natural Philosoph is currently offline  The Natural Philosoph
Messages: 993
Registered: September 2010
Karma: 0
Senior Member
oldyork90 wrote:
> Is there a way to localize a var inside a block? Can I have this
> behavior without calling a function?
>
>
> $myA = 'A'
>
> {
> $myC = 'C';
> $myA = 'B';
> echo $myA . ' says B';
> }
>
> echo $myA . ' says A';
> echo $myC .... does not exist
>
>
> Thank you.
yeah,. Write the program in C :-)
Re: scoping inside a block [message #176545 is a reply to message #176349] Mon, 09 January 2012 13:31 Go to previous messageGo to next message
Gregor Kofler is currently offline  Gregor Kofler
Messages: 69
Registered: September 2010
Karma: 0
Member
Am 2011-12-27 21:08, schrieb oldyork90:
> Is there a way to localize a var inside a block?

No.

Gregor
Re: scoping inside a block [message #176555 is a reply to message #176545] Mon, 09 January 2012 14:49 Go to previous message
The Natural Philosoph is currently offline  The Natural Philosoph
Messages: 993
Registered: September 2010
Karma: 0
Senior Member
Gregor Kofler wrote:
> Am 2011-12-27 21:08, schrieb oldyork90:
>> Is there a way to localize a var inside a block?
>
> No.
>

Strictly, no. In practice, make that block a function?

The overhead on a call return is not great.
Even if its only ever used once.

There is a lot to be said for making any long bit of code into a series
of calls to small functions to improve isolation, and separate
different logical flows.

So instead of if (this)
{
// Do pages of stuff...
}
eslsee {
// Do more pages of stuff
}


becomes if (this)
{
Do_pages_of_stuff($withThis, $andThis..);
}
else {
Do_more_pages_of_stuff($withThis, $andThis..);
}


etc etc.

I certainly find it helps me keep track of what the heck I am trying to
do a bit easier anyway.


The other way is of course massive documentation and commenting as we
used to do with Assembler, where the logical flow was completely
obscured by the actual language statements.

A block statement at the top explaining the flow, and comments
throughout like
;*********************
; In this section we compare gross profit with gross interest on loans
; Register A is the loan, B is the profit, in pence.
;*******************************************************

CMP A,B ; If the net cash flow is equal to the interest
JMPLT NO_PROFIT ; THEN prepare shareholder excuses
; ELSE etc ..


Which is megabytes of explanation for every few kilobytes of actual code..

The joy of discovering portable assembler AKA 'C' was...considerable.

Don't expect too much of your language: Just consider the alternatives....

> Gregor
  Switch to threaded view of this topic Create a new topic Submit Reply
Previous Topic: transfering all MySQL rows to an array
Next Topic: Trouble with sending e-mail from simple php script
Goto Forum:
  

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ]

Current Time: Sun Nov 24 18:31:21 GMT 2024

Total time taken to generate the page: 0.02149 seconds