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
1

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

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)
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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.