1

I would like a quick help with this. Don't know where I am missing something. I am trying to add new schools into an array called Schools with a function, so that whenever someone passes in a value, it becomes part of the array for schools.

See the Code:

$schools = array("Johnson's College", "Oxford School of Ham");
echo count($schools);

function addSchools ($newSchool){
     $schools[count($schools)] = $newSchool;
     return $newSchool;
}
addSchool("Harvard Business School");

foreach($schools as $key){
  echo $key;
}

The foreach statement is just to verify that the new school: "Havard Business..." was added. But it is giving an error.

Any to help fix that, so that when I use the foreach to loop through the array, I will see everything without error.

Thanks.

5
  • 1
    It sounds like you want array_push(). See the documentation Commented Jan 26, 2016 at 6:49
  • 1
    What error are you getting? Commented Jan 26, 2016 at 6:49
  • 1
    foreach statements never pre-started or post-ended with curly brackets Commented Jan 26, 2016 at 6:54
  • 1
    Function will not change $schools variable. Read about scopes. Commented Jan 26, 2016 at 6:54
  • @Louis you should use array_push() see my answer.. You dont need to create another function to add another value to your array because php already had array_push. Commented Jan 26, 2016 at 7:01

4 Answers 4

2

Just add using:

$schools[]= $newSchool;

Or using array_push function

If you use array_push() to add one element to the array it's better to use $array[] = because in that way there is no overhead of calling a function.

You could make $schools var global or pass it by reference:

function addSchools (&$newSchool){
     $schools[count($schools)] = $newSchool;
     return $newSchool;
}

Passing values by reference

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

3 Comments

Yeah I tried $schools[]= $newSchool before posting the comment, but still doesn't work...
Ok. So on a webpage where a user wants to show their school portfolio by adding more than one school, how would the array push help? Remember that there are different site users in this case.
array_push($schools,"school1","school2","school3"); :·) ?
0

Try using array_push(); This will add it to the end of the array.

$schools = array("Johnson's College", "Oxford School of Ham");
echo count($schools);

function addSchools ($schools, $newSchool){ array_push($schools, $newSchool); $schools[count($schools)] = $newSchool; return $newSchool; } addSchool("Harvard Business School");

{foreach($schools as $key){ echo $key; }

Comments

0

You dont need to create another function just to add another value to your array you can use array_push() like this.

<?php 

$schools = array("Johnson's College", "Oxford School of Ham");

array_push($schools, "Harvard Business School");

print_r($schools);

Comments

0

You call function addSchool("Harvard Business School"); But function name is addSchools. And in function addSchools variable $schools is global, so You function must be like this:

function addSchools ($newSchool){
global $schools;
$schools[count($schools)] = $newSchool;
return $newSchool;

}

3 Comments

This worked. But I wanted to ask, by using the keyword global just before the array $schools, won't that make the users school visible? I still wanted to keep it private to that user alone.
If yo dont want to use global, pass $schools to function as parameter: function addSchools ($newSchool, $schools){ $schools[count($schools)] = $newSchool; return $newSchool; addSchools("Harvard Business School", $schools);
@Louis: No, that's not what the keyword global is for. A defined variable outside of a function isn't available in said function. With the keyword global you make the variable in the function's scope available. The better option is to pass it as argument by reference. Read about references on php.net.

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.