call a php function when you need it [closed]

0

Good morning, I must make some checks to print "hello world" in case b = 1 ob = 2 I know that this can be done in an if with the operator or but the teacher wants the function printed by the "hello world" is called in the if of b = 1 or in the other if of b = 2

the question is how to call it there within the if, what syntax should follow?

thanks!

    
asked by Gabriel Uribe Gomez 10.10.2017 в 22:23
source

2 answers

1

The question is not very clear, I would recommend that for a future occasion at least put some pseudo-code, if I translate literally what you are asking would be something like this:

<?php

function sayHelloWord($number) {
    echo "Hello Word ($number) \n";
}

# Genera un número entre 0 y 5
$b = rand(0, 5);

if ($b == 1) {
    sayHelloWord($b);
} elseif ($b == 2) {
    sayHelloWord($b);
} else {
    echo "El numero es: " . $b ."\n";
}

The way I think the majority would think it would be:

<?
...
$values = [1,2];

if (in_array($b, $values)) {
    sayHelloWord($b);
} else {
    echo "El numero es: " . $b ."\n";
}
...

If the answer is not here, then it would be "healthy" to publish what you have done and ask for help specifically in the segment of the code that does not see well.

    
answered by 10.10.2017 в 23:28
0

As far as I understand, you should make the two comparisons, then you could do it in the following way:

$b = 0; //Le estableces el valor que desees
if ($b == 1){
    echo "Hello World";
}elseif($b == 2){
    echo "Hello World";
}else{
    //Hacer otra cosa
}
    
answered by 10.10.2017 в 23:04