Funzione
echo '<form action="function.php#tbox1" method="post">
<input type="text" name="numOne" value="'.($_POST['numOne']?$_POST['numOne']:"Scrivi un numero").'">
<input type="text" name="numTwo" value="'.($_POST['numTwo']?$_POST['numTwo']:"Scrivi un numero").'">
<input type="submit" value="Invia" >
</form>';
function test($parOne, $parTwo){
if(is_numeric($parOne) && is_numeric($parTwo)){$sum=$parOne+$parTwo; $string = 'la somma dei due numeri è: '.$sum;}else{$string = 'Pls. inserisci due numeri!';}
return $string;
}
echo test($_POST['numOne'], $_POST['numTwo']);
definizione per recursione click: vedi codice
$folderPath = "../step_prog";
function readFolder( $path ) {
// Open the folder
if ( !( $dir = opendir( $path ) ) ) die( "Can't open $path" );
$filenames = array();
// Read the contents of the folder, ignoring '.' and '..', and
// appending '/' to any subfolder names. Add all the files and
// subfolders to the $filenames array.
while ( $filename = readdir( $dir ) ) {
if ( $filename != '.' && $filename != '..' ) {
if ( is_dir( "$path/$filename" ) ) $filename .= '/';
$filenames[] = $filename;
}
}
closedir ( $dir );
// Sort the filenames in alphabetical order
sort( $filenames );
// Display the filenames, and process any subfolders
$string = '';
$string .= "<ul>";
foreach ( $filenames as $filename ) {
$string .= "<li>". $filename;
if ( substr( $filename, -1 ) == '/' ){$string .=' '. readFolder( $path."/" . substr( $filename, 0, -1 ) );}
$string .= "</li>";
}
$string .= "</ul>";
return $string;
}
echo "<h2>file e cartelle in ".$folderPath.":</h2>";
echo readFolder( $folderPath );
ricorsione: un classico esempio: il fattoriale click: vedi codice
function factorialString($n){
$out = 0;
$comp = '';
$string = '';
if ($n == 0) {
$out = 1;
$comp = 1;
$string = "caso base: <br> 0. factorial(0) = 1, risultato è 1<br>";
}else{
$call = $n-1;
$out = ($n * (factorialString($call)[0]));
$comp = $n . '*' . factorialString($call)[1];
$string = factorialString($call)[2] .$n.". factorial(".$n.") = ".$n." * factorial(".$call.") = ". factorialString($call)[1].", risultato è ".$out."<br>";
}
$r = array($out,$comp,$string);
return $r;
}
echo factorialString(11)[2];
Traverse DOM elements recursively -
Click qui (Attento, lungo!)
var walkDom=function(html_elem, func)
{
if(typeof func!='function') return;
var loop=function(html_elem)
{
do
{
func(html_elem); // call callback for every element
if(html_elem.hasChildNodes())
loop(html_elem.firstChild);
}while(html_elem=html_elem.nextSibling);
};
loop(html_elem);
//loop(html_elem.firstChild); // do not include siblings of start element
}
var fn=function(html_elem)
{
alert(html_elem.outerHTML);
}
Funzione ricorsiva - Frattale
float theta;
float a = 0;
void setup() {
size(640, 360);
}
void draw() {
background(255);
frameRate(3);
stroke(240,0,0);
// Let's pick an angle 0 to 90 degrees based on the mouse position
if (a < 360){ a = a+5;
}else{ a=0; }
// Convert it to radians
theta = radians(a);
// Start the tree from the bottom of the screen
translate(width/2,height);
// Draw a line 120 pixels
line(0,0,0,-100);
// Move to the end of that line
translate(0,-100);
// Start the recursive branching!
branch(100);
}
void branch(float h) {
// Each branch will be 2/3rds the size of the previous one
h *= 0.66;
// All recursive functions must have an exit condition!!!!
// Here, ours is when the length of the branch is 2 pixels or less
if (h > 2) {
pushMatrix(); // Save the current state of transformation (i.e. where are we now)
rotate(theta); // Rotate by theta
line(0, 0, 0, -h); // Draw the branch
translate(0, -h); // Move to the end of the branch
branch(h); // Ok, now call myself to draw two new branches!!
popMatrix(); // Whenever we get back here, we "pop" in order to restore the previous matrix state
// Repeat the same thing, only branch off to the "left" this time!
pushMatrix();
rotate(-theta);
line(0, 0, 0, -h);
translate(0, -h);
branch(h);
popMatrix();
}
}
PHP is a server-side scripting language designed for web development but also used .....
wikipediaJavaScript ("JS" for short) is a full-fledged dynamic programming language that, when applied to an HTML document, can provide dynamic interactivity on websites. It was invented by Brendan Eich, co-founder of the Mozilla project, the Mozilla Foundation, and the Mozilla Corporation. JavaScript is incredibly versatile. You can start small, with carousels, image galleries, fluctuating layouts, and responses to button clicks. With more experience, you'll be able to create games, animated 2D and 3D graphics, comprehensive database-driven apps, and much more!.....
MDN JavaScript basicsHello! p5.js is a JavaScript library that starts with the original goal of Processing, to make coding accessible for artists, designers, educators, and beginners, and reinterprets this for today's web.
p5.jsAccademia Statale Belle Arti Albertina Torino | ABTEC40 - Chiari- a.a.24.25| Thanks to http://templated.co.