Question: I need one help.I need to push one array object value into another array object in each iteration using PHP.I am explaining my code below.

```
for($i=0;$i<$len;$i++){
 while($report=mysqli_fetch_assoc($reportqry)){
        $result[]=$report;
    }
  //$arry
}

```

Here i need the `$result` will push into `$arry` in each iteration of the loop.Please help me.

Answer: 
You have to remove the semicolon (`;`) after the temination condition of your while loop. `;` is the end of an statement, after that a new statment beginns. Apart from this your `return` statement would be inside the `while` loop. Remove it outside the loop. Adapt your code like this:

```
int len(char p[]) {
    int count = 0;
    while (*p != '\0')
    {
        count++;
        p++;
    }
    return count;
}

```

Further you have to read a string from input into your dynamicly allocated array of `char`:

```
int main() {

    int array_size = 100;
    char *p=new char[array_size];      // allocate memory

    cout << "Type a phrase" << endl;
    cin >> p;                          // read string into allocated memory

    cout << "Your phrase was " << len(p) << " characters long." << endl;

    delete p;                          // free memory
    return 0; 
}

```

The function `strlen` gives you the length of a `\0`-terminated string.

```
#include <string.h>

int main() {

    int array_size = 100;
    char *p=new char[array_size];     // allocate memory

    cout << "Type a phrase" << endl;
    cin >> p;                         // read string into allocated memory

    cout << "Your phrase was " << strlen(p) << " characters long." << endl;

    delete p;                         // free memory
    return 0; 
}

```

**But I recommend to use `std::string`:**

```
#include <string>

int main() {

    std::string str;
    cout << "Type a phrase" << endl;
    cin >> str;

    cout << "Your phrase was " << str.size() << " characters long." << endl;
    return 0; 
}

```

`std::string` represents a sequences of characters with dynamic length.
**Getting the size of dynamically allocated array**

A dynamically allocated array does not have any information about its size that is available in a standards compliant way.

We are able to compute the length of C style strings since there is sentinel element, `'\0'`, to mark the end of the string. There are no such elements for other types.

Even then, you cannot compute the size of an array of characters allocated using heap memory like you have.

**Problems with posted code**

1. You have the following lines outside all functions.

```
int array_size;
typedef char* charPtr;
charPtr a = new char[array_size];
char *p=a;

```

They are executed before anything in `main` gets executed. When these lines are executed, `array_size` gets initialized to `0`. Then you allocate memory for `charPtr` using `0` as the value of `array_size`.
2. Function `len` has an error due to a typo, which could lead to either (a) a hanging program or an incorrect return value.

```
int len(char p[])
{
   int count = 0;

   while (*p != '\0');
               //   ^^^ The semicolon is a problem
   {
      count++;
      p++;
      return count;
   }
}

```

If `*p` is not equal to `'\0'`, the program will never get out of the `while` statement. It will hange.

If `*p` is equal to `'\0'`, the program will get out of the `while` statement but it will still execute the the lines after that. As a consequence, you will end up returning `1` as the length where `0` is the right answer.
3. In `main`, you have:

```
cout << "Type a phrase" << endl;
cin >> array_size;

```

When the user sees the output, they will try to enter a phrase. However, `array_size` is an `int`. There is a mismatch between the prompt to the user and the line to read the data.

You could change it them to:

```
cout << "Type array size" << endl;
cin >> array_size;
cout << "Type a phrase" << endl;

```

That is one step better but that still does not change the fact that memory for `a` was allocated using a size of `0`.
4. Using uninitialized memory

You are calling `len(p)` in the `cout` line but the elements of `p` have not been initialized.

It's not clear from your post what the program is supposed to do. I am guessing that you want to read a phrase from `stdin` and write it out to `stdout`. You can use the following simplified version for that.

```
int main() 
{
   std::string phrase;
   cout << "Type a phrase" << endl;

   // Get the entire line as a phrase.
   std::getline(std::cin, phrase);

   cout << "Your phrase is " << phrase << endl;
   cout << "It is " << phrase.size() << " characters long." << endl;

   return 0;
}

```