PHP parametric functions

3

I'm new to php and I'm creating a comparison of box sizes, and for that I'd like to put inside a function but I do not know which parameter to put inside the function's relatives. Someone could help me, Thanks.

<?php 
include("caixa.php");
function CalcularCaixa()// não sei o que colocar dentro desse parentes
{
    if(isset($_SESSION['caixa'][$id]));
                    {   
                        if($id == 1)
                        {
                            if($quantidade >= 1)
                            {   
                                if($quantidade <= 50)
                                {
                                    echo $altura;
                                    echo $largura;
                                    echo $comprimento;
                                    echo $peso; 
                                }
                                elseif($quantidade <= 100)
                                {
                                    echo $altura * 2;
                                    echo $largura * 2;
                                    echo $comprimento * 2;
                                    echo $peso * 2;     
                                }
                                elseif($quantidade <= 150)
                                {
                                    echo $altura * 3;
                                    echo $largura * 3;
                                    echo $comprimento * 3;
                                    echo $peso * 3;     
                                }
                            }

                        }

                    }

}
?>
    
asked by anonymous 21.05.2015 / 17:49

2 answers

4

In% of the function you pass the necessary parameters so that your function executes a certain task.

Example: You have the function () , which will add 2 numbers.

1st the function will add 2 numbers, so we need to get these 2 numbers so that the function can sum, so we can pass these 2 numbers per parameter.

<?php
public function($num1, $num2){
    echo "A soma dá: ".$num1 + $num2;
}

// usando a função;
soma(1,2);
?>

I recommend that you read on these links, to better understand the passing of parameters:

PHP: Function Arguments

Functions: passing parameters

Passing Parameters

It's also very useful to visit the codeacademy of php that is currently in Portuguese, for beginners is excellent because it covers everything from declaring a variable to more complex functions, and can help you in the long run.

    
21.05.2015 / 18:04
1

In your case I would pass array $caixa to the desired attributes:

<?php

function CalcularCaixa(array $caixa)
{
    if ($caixa['quantidade'] >= 1) {
        if ($caixa['quantidade'] <= 50) {
            return $caixa;
        } elseif ($caixa['quantidade'] <= 100) {
            $caixa['altura'] *= 2;
            $caixa['largura'] *= 2;
            $caixa['comprimento'] *= 2;
            $caixa['peso'] *= 2;
        } elseif ($caixa['quantidade'] <= 150) {
            $caixa['altura'] *= 3;
            $caixa['largura'] *= 3;
            $caixa['comprimento'] *= 3;
            $caixa['peso'] *= 3;
        }
    }

    return $caixa;
}

$caixa = $_SESSION['caixa'][1];
$caixaCalculada = CalcularCaixa($caixa);

The idea is to think about how to reuse the logic elsewhere in the application, in this case in other boxes.

    
21.05.2015 / 18:14