简体   繁体   中英

Variable trouble in php

I am trying to make an automated function to make "Cards" on my website and for some reason It says I am not defining an array on line 18 even though it is defined on 3.

Here is the error:

Notice: Undefined variable: cardArray in 
E:\XAMPP\htdocs\Websites\website\pages\featured.php on line 18

Notice: Undefined variable: cardArray in 
E:\XAMPP\htdocs\Websites\website\pages\featured.php on line 18

And here is the code:

<?php

$cardArray = array();   

newCard("Eltit", "This is some interesting text that everyone wants to read because it is awesome. The said this is, is that people might not be able to comprehend it.", "James.png");
newCard("Eltit 2!", "This is just some more interesting works to feast your eyes on mainly because this website doesn't have any content to fill it up yet :)");

for ($i=1; $i < count($cardArray); $i++) { 
    makeCard($cardArray[i][0], $cardArray[i][1], $cardArray[i][2]);
}

//<<--    Functions    -->>//

function newCard($title = "Title", $text = "Text", $avatar = "DefaultUserIcon.png") {

    $tempArray = array($title, $text, $avatar);

    $cardArray[count($cardArray)+1] = $tempArray;

}

function makeCard($title, $text, $avatar) {

    echo "
        <div class='box'>
            <div id='profile' style='background: url('../img/avatars/".$avatar."');'>
                <div id='avatar'></div>
                <div id='info'>
                    This is where the user info will go.
                </div>
            </div>
            <div id='content'>
                <div id='description'> 
                    <h1>".$title."</h1>
                    ".$text."
                </div>
            </div>
            <div id='footer'>
                <div style='padding: 18px;'>
                    This is where the buttons will be.
                </div>
            </div>
        </div>";

}
?>

Your $cardArray variable is in the global scope. In order to use in a function, you need to declare it as global inside the function:

function newCard($title = "Title", $text = "Text", $avatar = "DefaultUserIcon.png") {
    global $cardArray;

    $tempArray = array($title, $text, $avatar);
    $cardArray[count($cardArray)+1] = $tempArray;
}

Although this fixes your problem. It's advised not to use global and is recommended to pass them into the function as a parameter.

Example:

function newCard($title = "Title", $text = "Text", $avatar = "DefaultUserIcon.png", $cardArray) {
    $tempArray = array($title, $text, $avatar);
    $cardArray[count($cardArray)+1] = $tempArray;
}

Then all the callers would pass the $cardArray to the newCard function as:

newCard("Eltit", "This is some interesting text that everyone wants to read because it is awesome. The said this is, is that people might not be able to comprehend it.", "James.png", $cardArray);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM