PHP parametric functions

Asked

Viewed 70 times

3

I am new in php and I am creating a comparison of box sizes, and for that I would like to put inside a function more I do not know which parameter to put inside the relatives of the function. Someone could help me, Thank you.

<?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;     
                                }
                            }

                        }

                    }

}
?>

2 answers

4


Us () of the function you pass the parameters necessary for your function to perform certain task.

Example: You have the function Soma, that will add 2 numbers.

1º the function will sum 2 numbers, so we need to get these 2 numbers so that the function can do the sum, then 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 a read on these links, to better understand the passage of parameters:

PHP: Function Arguments

Functions: passing parameters

Parameter Crossing

It is also very useful to visit the codeacademy de php that is currently in Portuguese, for beginners is excellent, since it covers from the declaration of a variable to more complex functions, can help you in the long term.

  • 1

    Thanks for the help man, I’ll take a look and try here thanks!

1

In your case I’d pass one array $caixa with 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 how to reuse logic elsewhere in the application, in this case in other boxes.

  • Thanks for the idea I hadn’t thought of for an array vlw!

  • @Leonardosilva if any answer has solved your question, you can mark it as accepted. Read more on [tour].

Browser other questions tagged

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