PHP – Curso-Usando funções-01

usandofuncoes01


usandofuncoes02


Códigos

<! usandofuncoes.php >
<!doctype html>
<html lang=”pt_br”>
<head>
<title>ArraysMult</title>
<meta charset=”utf-8″>
<style type=”text/css”></style>
</head>
<?php
$norm = “</strong><font size=’2′ font color = ‘#000000′>”;
$negr= ” <strong><font size=’4′ face=’verdana’ font color = ‘#191970′>”;
$negred= ” <strong><font size=’4′ face=’verdana’ font color = ‘#800000’>”;
//
echo “$negr”;
echo “<h1> Cadastro</h1>”;
echo “$norm<hr>”;

echo date(“d/m/Y”);
$dat=date(“d/m/Y”);
?>
<br>
<input type=”button” onclick=”alert(‘Olá Mundo!’)” value=”Click”>
<br><br>

<form action=”usandofuncoes2.php” method=”post”>
Valor: <input type=”text” name=”vr” value=0>
Data: <input type=”date” name=”bday” max=”2016/01/30″><br>

<input type=”radio” name=”tipo” value=”PF” checked>PF<br>
<input type=”radio” name=”tipo” value=”PJ”>PJ<br>
<hr>

<input type=”checkbox” name=”vehicle1″ value=”Bike”>Biciclate
<br>
<input type=”checkbox” name=”vehicle2″ value=”Car”>Carro
<br><br>

Cor: <input type=”color” name=”favcolor” value=”#ff0000″>

Tamanho Fonte: <input type=”number” name=”quantity” min=”1″ max=”7″ step=”1″ value=”4″>

<input type=”submit”>
</form>
<br>
===============================================

<! usandofuncoes2.php >
<!doctype html>
<html lang=”pt_br”>
<head>
<title>ArraysMult</title>
<meta charset=”utf-8″>
<style type=”text/css”></style>
</head>

<?php
$norm = “</strong><font size=’2′ font color = ‘#000000′>”;
$negr= ” <strong><font size=’4′ face=’verdana’ font color = ‘#191970′>”;
$negred= ” <strong><font size=’4′ face=’verdana’ font color = ‘#800000’>”;
//
echo “$negr”;
echo “<h1> Cadastro</h1>”;
echo “$norm<hr>”;
//
include (“extenso.php”);
$valor = $_POST[“vr”];
//$dt = strtotime(“d-m-Y”, $_POST[“bday”]);
$dt = date(‘d/m/Y’, strtotime($_POST[“bday”]));

$dim = extenso($valor);
$dim = ereg_replace(” E “,” e “,ucwords($dim));

$valor = number_format($valor, 2, “,”, “.”);
echo “<strong><font size=’4′ face=’Verdana’>”;
echo “<font color=” . $_POST[“favcolor”]. “>”;
echo “<font size=” . $_POST[“quantity”]. “>”;
echo $dt . ” – Tipo de Pessoa: ” . $_POST[“tipo”] . “<br>”;
echo “R$ $valor” . ” (” .$dim .”) <br>”;
// date(‘d-m-Y’,$xdata);
echo “<br>”;
echo “Veículos: “;
//$veic1 = $_POST[“vehicle1”];
//$veic2 = $_POST[“vehicle2”];
//echo $veic;
if ( isset($_POST[“vehicle1”]) )
{
echo $_POST[“vehicle1”] . “, “;
}
else
{
echo “”;
}

if ( isset($_POST[“vehicle2”]) )
{
echo $_POST[“vehicle2”];
}
else
{
echo “”;
}
?>
========================================

Função  Extenso
<?php
function extenso($valor = 0, $maiusculas = false) {

$singular = array(“centavo”, “real”, “mil”, “milhão”, “bilhão”, “trilhão”, “quatrilhão”);
$plural = array(“centavos”, “reais”, “mil”, “milhões”, “bilhões”, “trilhões”,
“quatrilhões”);

$c = array(“”, “cem”, “duzentos”, “trezentos”, “quatrocentos”,
“quinhentos”, “seiscentos”, “setecentos”, “oitocentos”, “novecentos”);
$d = array(“”, “dez”, “vinte”, “trinta”, “quarenta”, “cinquenta”,
“sessenta”, “setenta”, “oitenta”, “noventa”);
$d10 = array(“dez”, “onze”, “doze”, “treze”, “quatorze”, “quinze”,
“dezesseis”, “dezesete”, “dezoito”, “dezenove”);
$u = array(“”, “um”, “dois”, “três”, “quatro”, “cinco”, “seis”,
“sete”, “oito”, “nove”);

$z = 0;
$rt = “”;

$valor = number_format($valor, 2, “.”, “.”);
$inteiro = explode(“.”, $valor);
for($i=0;$i<count($inteiro);$i++)
for($ii=strlen($inteiro[$i]);$ii<3;$ii++)
$inteiro[$i] = “0”.$inteiro[$i];

$fim = count($inteiro) – ($inteiro[count($inteiro)-1] > 0 ? 1 : 2);
for ($i=0;$i<count($inteiro);$i++) {
$valor = $inteiro[$i];
$rc = (($valor > 100) && ($valor < 200)) ? “cento” : $c[$valor[0]];
$rd = ($valor[1] < 2) ? “” : $d[$valor[1]];
$ru = ($valor > 0) ? (($valor[1] == 1) ? $d10[$valor[2]] : $u[$valor[2]]) : “”;

$r = $rc.(($rc && ($rd || $ru)) ? ” e ” : “”).$rd.(($rd &&
$ru) ? ” e ” : “”).$ru;
$t = count($inteiro)-1-$i;
$r .= $r ? ” “.($valor > 1 ? $plural[$t] : $singular[$t]) : “”;
if ($valor == “000”)$z++; elseif ($z > 0) $z–;
if (($t==1) && ($z>0) && ($inteiro[0] > 0)) $r .= (($z>1) ? ” de ” : “”).$plural[$t];
if ($r) $rt = $rt . ((($i > 0) && ($i <= $fim) &&
($inteiro[0] > 0) && ($z < 1)) ? ( ($i < $fim) ? “, ” : ” e “) : ” “) . $r;
}

if(!$maiusculas){
return($rt ? $rt : “zero”);
} else {

if ($rt) $rt=ereg_replace(” E “,” e “,ucwords($rt));
return (($rt) ? ($rt) : “Zero”);
}

}

?>