function()){})<?php
function hello() {
echo "Hello, world!<br>";
}
// Équivalent en Java
public class Main {
public static void hello() {
System.out.println("Hello, world!");
}
}
())<?php
function hello() {
echo "Hello, world!<br>";
}
hello(); // Affiche "Hello, world!"
hello(); // Affiche "Hello, world!"
hello(); // Affiche "Hello, world!"
// Équivalent en Java
public class Main {
public static void hello() {
System.out.println("Hello, world!");
}
public static void main(String[] args) {
hello(); // Affiche "Hello, world!"
hello(); // Affiche "Hello, world!"
hello(); // Affiche "Hello, world!"
}
}
,)<?php
function hello($name) {
echo "Hello, $name!<br>";
}
hello("Alice"); // Affiche "Hello, Alice!"
hello("Bob"); // Affiche "Hello, Bob!"
public class Main {
public static void hello(String name) {
System.out.println("Hello, " + name + "!");
}
public static void main(String[] args) {
hello("Alice"); // Affiche "Hello, Alice!"
hello("Bob"); // Affiche "Hello, Bob!"
}
}
return<?php
function square($x) {
return $x * $x;
}
$result = square(3);
echo $result; // Affiche 9
// Équivalent en Java
public class Main {
public static int square(int x) {
return x * x;
}
public static void main(String[] args) {
int result = square(3);
System.out.println(result); // Affiche 9
}
}
<?php
function hello($name = "world") {
echo "Hello, $name!<br>";
}
hello(); // Affiche "Hello, world!"
hello("Alice"); // Affiche "Hello, Alice!"
// Équivalent en Java
// Il n'est pas possible de définir des paramètres optionnels en Java.
// Ceci est spécifique à PHP.
,)<?php
function add($x, $y) {
return $x + $y;
}
$result = add(3, 5);
echo $result; // Affiche 8
// Équivalent en Java
public class Main {
public static int add(int x, int y) {
return x + y;
}
public static void main(String[] args) {
int result = add(3, 5);
System.out.println(result); // Affiche 8
}
}
<?php
function square($x) {
return $x * $x;
}
echo $x; // Erreur : variable $x non définie
// Équivalent en Java
public class Main {
public static int square(int x) {
return x * x;
}
public static void main(String[] args) {
System.out.println(x); // Erreur : variable x non définie
}
}
global<?php
$x = 42;
function square() {
global $x;
$x = $x * $x;
}
square();
echo $x; // Affiche 1764
// Équivalent en Java
public class Main {
public static int x = 42;
public static int square() {
x = x * x;
}
public static void main(String[] args) {
square();
System.out.println(x); // Affiche 1764
}
}
<?php
$length = strlen("Hello, world!");
echo $length; // Affiche 13
// Équivalent en Java
public class Main {
public static void main(String[] args) {
String s = "Hello, world!";
int length = s.length();
System.out.println(length); // Affiche 13
}
}
abs, sqrt, pow, min, max, etc.sqrt<?php
$result = sqrt(16);
echo $result; // Affiche 4
// Équivalent en Java
public class Main {
public static void main(String[] args) {
double result = Math.sqrt(16);
System.out.println(result); // Affiche 4.0
}
}
strlen, substr, str_replace, strtolower, strtoupper,
etc.strupper$result = strtoupper("hello, world!");
echo $result; // Affiche "HELLO, WORLD!"
// Équivalent en Java
public class Main {
public static void main(String[] args) {
String result = "hello, world!".toUpperCase();
System.out.println(result); // Affiche "HELLO, WORLD!"
}
}
isset, empty, unset, is_array, is_string, etc.isset<?php
$var = 42;
if (isset($var)) {
echo "The variable is defined.";
} else {
echo "The variable is not defined.";
}
echo "<br>"; // Retour à la ligne HTML
if (isset($undefined)) {
echo "The variable is defined.";
} else {
echo "The variable is not defined.";
}
// Équivalent en Java
public class Main {
public static void main(String[] args) {
int var = 42;
if (var != null) {
System.out.println("The variable is defined.");
} else {
System.out.println("The variable is not defined.");
}
System.out.println(); // Retour à la ligne
int undefined;
if (undefined != null) {
System.out.println("The variable is defined.");
} else {
System.out.println("The variable is not defined.");
}
}
}
require<?php
// Fichier `functions.php`
function hello($name) {
echo "Hello, $name!<br>";
}
<?php
// Fichier `index.php`
require "functions.php"; // On inclut le fichier
// La fonction `hello` est définie dans le fichier importé
// et peut être utilisée ici
hello("Alice");
include et requireinclude et requireinclude : si le fichier n'est pas trouvé, un avertissement est émisrequire : si le fichier n'est pas trouvé, une erreur fatale est émiserequireEst-ce que vous avez des questions ?
Pour le mini-projet ou les exercices, n'hésitez pas à vous entraidez si vous avez
des difficultés !
URLs
Illustrations