What if you are unable to work with arrays while building PHP extensions?
It will be like you have missed half of the picture, I guess.
Before discussing how you can handle and create arrays when building PHP Extensions, I will discuss what are arrays and how are they used in c.
Array is block of memory.
Some time it is said that an array is pointer, which is not true in all cases. Array can act as pointer and pointer as an array, however they are different.
To declare an array in c, simply write
This num now represent block of memory. The size id 5, so it take 20 bytes, keep in mind that an integer hold 4 bytes.
To assign value to first index of the array, write
*num = 10;
Although this can be done, however this is usually achieved by writing
num = 10;
The above two statements are identical. However one is done using pointers and the other using array index.
To give value to second index, write
*(num+1) = 20;
num = 20;
Let’s take our discussion a bit further.
To have an access to array address, we can simply do like this
p = num;
First we declare an integer pointer, and then assign it an array. It means that the integer pointer p hold the memory address of the first element of array num.
This can also be achieved by writing
p = & num;
p now hold the address of the first element of the array.
Okay, while taking about an array and pointers earlier, I said they are a bit different although they can act as each other. Consider the following example;
double *dp = num;
If you want to access the fourth element of the array, write
This is similar to
With pointer, however you can done this
it means, move to the next memory address. However
can not be done, because num is an array not a pointer. This will not take you to the next memory address.
Well, when it come to passing an array as argument to a function, this can be achieved using the following methods in c.
void f(long *lp)
In the first function we define it as taking the address of the array using pointer. Changing the values of the pointer location will modify the original array.
void f(long l)
In this example we take array as an array. This array is now local to the function, so changing it doesn’t cause the original array to be modified.
Although you can define the size of the array, however better approach is to leave it empty like
void f(long l)
The compiler will keep track of the size of the array.
Now let’s have a look at how you can better use array while building extensions.
Array can easily be passed as parameters and returned from function.
Look at the following code which build and return an array.
add_index_long(return_value, 100, 33);
// to add sub array to the array
add_next_index_string(subarr, “sub array”,1);
While working with the array, you will first need to initialize an array as
Once array is initialized, you can add values to it. Several methods are used for this purpose.
add_next_index_*() method is used to add value to next available method.
add_index_*() is used to add value to given index.
add_assoc_* is used to add value to key. This is used for creating an associated value.