Hidden Friend with PHP

Asked

Viewed 93 times

1

Personal I am creating a logic for a hidden friend in PHP.

I’m using the code below to accomplish this:

//Definindo nomes para participar do amigo oculto
$nomes=["Matheus","Alice","Claudia","Leo","Emanuel","Joao"];

//Guardo a primeira formação para definir quem irá tirar quem no amigo oculto;
$ori = $nomes;

//Embaralho de forma aleatória os nome mandados. Sempre serão aleatórios
shuffle($nomes);

foreach($nomes as $key => $n) echo str_pad(strtoupper($ori[$key]),"12"," ",STR_PAD_RIGHT) . ' ==> ' . strtoupper($n) . '<br>';

My question is this::
When I use foreach(shuffle($nomes) as $key => $n) i have an error. Why can’t I put function inside the foreach? How can I practice this?

  • Can you tell me what the error would be ?

  • Invalid argument supplied for foreach(); the Error I could understand. But I didn’t understand why it doesn’t accept. Where I have more information about this foreach?

  • 2

    From what I understand of foreach, it takes only array as parameter, already the shuffle returns a boleano, thus being the reason for the presentation of this error, follows a paragraph about the foreach: 'The foreach constructor provides an easy way to iterate over arrays. The foreach works only on arrays and objects, and will emit an error when trying to use it in a variable with a different data type or in an uninitialized variable.'

  • It turns out that the function shuffle changes the allocated array in memory. It does not return a new array. The function only returns if it has successfully done so (true) or not (false)

1 answer

2


When you create a variable in php or in any other language, this is stored in memory with a reference so that you can use when needed.

Most variables in php have local scope, which means if you do this...

$a = ['um', 'dois']; 
alterar($a);
print_r($a);

function alterar($a){
    $a[0] = "1";
    $a[1] = "2";
}

... the array $a will continue to have the value ['um', 'dois'] because in function, it is in a different scope.

So, like the shuffle ago?

In php, for you to change a variable into a function of another scope, you need to use an assignment by reference with the sign of &.

This means creating a new variable with the same reference. See an example:

$a = "olá"; // ola
$b =& $a; // ola

$b = "mundo"; // mundo
echo $a; // mundo

The output will be "world". Because both variables have the same reference.

An example in function would be like this:

$a = ['um', 'dois']; 
alterar($a); // altera
print_r($a);

function alterar(&$a){ // <-- aqui
    $a[0] = "1";
    $a[1] = "2";
}

Now the way out will be:

Array ( [0] => 1 [1] => 2 )

As @henriquuepedro said, the error happens because shuffle does not generate a new array. It just changes the allocated array and returns a bolean.

  • 4

    Now I understand, thank you very much. tbm to colleague @henriquuepedro. foreach will always expect an array. Thank you very much. Now it’s very clear!

  • 2

    @Leandroalfredo exactly! Quiet friend =)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.