<?php $a = 20; function myfunction($b) { $a = 30; global $a, $c; return $c = $b + $a; } print myfunction(40) + $c;
<?php if ($_POST["textar"]) { $a = $_POST["textar"]; } else { die("Заповніть будь ласка поле форми !"); } function myfunction($a) { $needarr = array(); //створюємо пустий масив, куди будемо записувати введені слова $expl = explode(' ', $a); //розбиваємо введену строку на масив (в якості розділювача - пробіл) foreach ($expl as $item) { $needarr[] = $item; } $rezult = array_unique($needarr); //перевіряємо унікальність введених слів $count = count($rezult); return $count; } echo myfunction($a);
<?php function myfunction($num, $msg) { for ($i = 0; $i != $num; $i++) { echo $msg . "<P>"; } } echo "This is before the function is called<P>"; myfunction(5, "This is a function with parameters"); echo "This is after the function has been called"; echo "<P>";
<?php include 'myfunction.php'; $returnedarray = myfunction(); for ($i = 0; $i < count($returnedarray); $i++) { echo $returnedarray[$i]; }
<?php // What would you replace ??????? with, below, to make the string Hello, World! be displayed? function myfunction() { /* ??????? */ print $string; } myfunction("Hello, World!"); /* 1) There is no way to do this 2) $string = $argv[1]; 3) $string = $_ARGV[0]; 4) list($string) = func_get_args(); OK or func_get_arg(0) 5) $string = get_function_args() */
<?php $b = 90; //$a = date("Y-m-d"); echo $a; $b = md5("edison0000916"); echo $b; echo "<br>"; //自定义函数 //即可以默认传值同时 给定初始的值 如果不定义的话就使用初始的值进行计算。 function myfunction($val, $val2 = "baby") { global $b; echo $val . " " . $val2; } myfunction("come on", "honey"); echo "<br>"; myfunction("come on"); echo "<br>"; if (function_exists("myfunction")) { echo "yes"; } else { echo "no"; } //返回引用值
header("Content-Type: text/html; charset=utf-8"); /* Declaración de la variables. */ $cadena = "Página de prueba."; /* Impresión en pantalla. */ print $cadena . "<br>"; /* Imprime un mensaje en pantalla ('print') y añade un salto de línea ('<br>'). El punto se utiliza para concatenar ('.'). */ /* También se puede concatenar con la variable sin el punto. Basta con meter la variable dentro de la cadena. */ print "{$cadena} Programación en php."; /* 'echo' también imprime. */ echo "<br> Archivo de ejemplos."; /* A diferencia de 'print', que no lo permite, 'echo' permite imprimir varias variables. */ $cadena2 = "Cadena dos."; echo "<br>"; echo $cadena, $cadena2; /* Declaración de funciones en php. */ function myfunction() { echo "<br><br>"; echo "Dentro de la función."; } /* Llamamos a la función. */ myfunction(); /* Podemos crear funciones en archivos php externos, y llamarlas haciendo un 'include' o un 'require' del archivo. La diferencia entre una y otra es que 'include' si no encuentra el archivo sigue con la ejecución del código (el flujo del código es de arriba a abajo), mientras que 'require' detiene el código si no encuentra el archivo. Tanto uno como otro pueden añadirse en cualquier punto del código; no es obligatorio llamarlos al principio. */
<?php session_start(); $font_size = 30; $text = myfunction(); $_SESSION["strings"] = $text; header('Content-type: image/png'); $image_width = 120; $image_height = 20; $image = imagecreate($image_width, $image_height) or die('Cannot Initialize new GD image stream'); $background = imagecolorallocate($image, 0, 255, 0); $black = imagecolorallocate($image, 0, 0, 0); imagestring($image, 5, 5, 1, $_SESSION["strings"], $black); imagejpeg($image); imagedestroy($image); function myfunction() { $chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; $numbers = '0123456789'; srand((double) microtime() * 1000000); $str = ""; $i = 0; while ($i < rand(10, 14) / 2) { $num = rand() % 33; $tmp = substr($chars, $num, 1); $tmp = $tmp . substr($numbers, $num, 1); $str = $str . $tmp; $i++; } return $str; }