1

I am making a test version of adding users to an array and need to find a way to add the user's name variable to my array.

I am using Repl.it's PHP Web Server, which means it runs in a browser (because Chrome OS) and just have PHP. My code looks somewhat like :

<html>
<body>

Welcome <?php echo $_POST["name"]; ?><br>
You will be added to a list of users
<?php
$usernames = array("John Kennedy", "Barrack Ohbama", "Abraham Lincon")
array_push ($usernames, $_POST["name"]);
for($x = 0; $x < $arrlength; $x++) {
    echo $usernames[$x];
    echo "<br>";
}
?>
</body>
</html>

But when I plug it in, I get:

172.18.0.1:51360 [500]: /list.php - syntax error, unexpected 'array_push' (T_STRING) in /home/runner/list.php on line 8
  • Possible duplicate of [PHP parse/syntax errors; and how to solve them?](https://stackoverflow.com/questions/18050071/php-parse-syntax-errors-and-how-to-solve-them) –  Jun 13 '19 at 23:45

2 Answers2

2

You're missing a semicolon:

$usernames = array("John Kennedy", "Barrack Ohbama", "Abraham Lincon");
#                                                                     ^

PHP is sometimes unclear about error messaging and takes some getting used to.

ggorlen
  • 44,755
  • 7
  • 76
  • 106
  • 1
    Adding to @ggorlen's answer, you have a var called $arrlength that is supposedly an integer, but it is never declared. Instead you probably want `count($usernames)` – symlink Jun 14 '19 at 02:51
0

You can always add new elements to array using array_push variant:

<?php
$usernames[] = $_POST["name"];
?>

If you want to set the key of new element, is also allowed

<?php
$usernames[$key] = $_POST["name"];
?>

https://php.net/manual/en/function.array-push.php

Benjamin
  • 558
  • 7
  • 15