Jan 29, 2013

PHP: Weird scope of variables

First of all, this post is simply a note that reminds me of something basic but important in PHP programming....


In PHP, except member variables of a class, all the PHP variables are available once it is assigned with some values even if it's surrounded by a set of brackets.

Generally, brackets in C/C++ has the effect of limiting the affective scope of a variable.
Considering the following two code segments of func1 and func2.

//test1.cpp
void func1()
{
    int a = 5;
    a++;
}

//test2.cpp
void func1()
{
    if(true)
    {
        int a = 5;
    }
    a++;
}

If you try to compile these segments, the compiler will throw the following error message:
    test.cpp: In function ‘void func()’:
    test.cpp:7: error: ‘a’ was not declared in this scope

As you can see, the declarations surrounded by a set of brackets will be treated as a local variable that lives and existed in the bracket.

But in PHP, the following code segments of func3 and func4 will execute without errors and and return the same value of 6.


function func3()
{
    $a = 5;
    $a++;

    return $a;
}
function func4()
{
    if(true)
    {
        $a = 5;
    }
    $a++;

    return $a;
}

 Base on theses examples, I have to be careful in writing PHP code and try to avoid mistakened variable rewriting......

Jan 4, 2013

The 'static' Keyword in C/C++

Recently I discovered that there're still a number of people doesn't know what does the keyword 'static' do exactly!!! Well then, here comes my tutorial of 'static'.

In brief, the static keyword does only one thing, it tells the program to allocate and maintain a number of fixed memory space to store the variable before the program start to execute, and treat the scope of static variables as the ordinary ones.

Jan 2, 2013

Objective-C Cookbook - 01 - Preparations

Here in this series of objective-c cookbook series, I am gonna arrange all my experiences in learning objective-c and present them in a more readable structure. I hope this will lower down the barrier of learning such an appealing language.

This cookbook is for ones who have programming experiences and already know the basics of programming languages, which means, some basic concepts like variable, basic types and operators will not be mentioned in this cookbook.

Jul 2, 2012

iOS Basic Sizes

Window 320 x 480 points
Status Bar height 20 points

Navigation Bar height 44 points
Navigation Bar Image 20 x 20 points

Tab Bar height 49 points
Tab Bar Icon 30 x 30 points

Portrait Keyboard height 216 points
Landscape Keyboard height 162 points

*points in iPhone4 = 2 pixels (retina display)


Jun 17, 2012

10000 digits of Euler's Number (constant e)

Well, this is an another constant that is used in many different places....
Hmm, I don't really like it! Because I have no feeling about this constant.



2.
7182818284 5904523536 0287471352 6624977572 4709369995

9574966967 6277240766 3035354759 4571382178 5251664274
2746639193 2003059921 8174135966 2904357290 0334295260
5956307273 1008532378 0527510636 8648701695 3141865527
4845908244 9550453392 8649764277 4136641659 6463663250
8736091584 1343970999 8317035382 3380092116 8146554153
7493054202 2246170932 1230949167 7634993111 3070302925
6989342067 6439191366 5038487357 8846610775 7255763079
2189886735 3790419412 0433774064 9490707386 3079049248
9764370698 3629736686 2198429250 7677002141 5740650029
3826954406 8718779542 7096976624 7465243666 2951385720
1920830317 7269234097 7016567453 9225777914 7341603684