Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 53395da

Browse files
committed
adding more functions
1 parent 7ef288f commit 53395da

File tree

4 files changed

+253
-0
lines changed

4 files changed

+253
-0
lines changed

‎array_map.php‎

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
<?php
2+
// array_map
3+
$names = [
4+
"reza",
5+
"hamid",
6+
"ali",
7+
"farzad",
8+
"reza",
9+
"abbas",
10+
"amirreza",
11+
"far"
12+
];
13+
14+
array_map(function($name) {
15+
echo strtoupper($name) . "\n";
16+
}, $names);
17+
18+
print "==============\n";
19+
20+
$my_function = function($name) {
21+
echo strtoupper($name) . "\n";
22+
};
23+
array_map($my_function, $names);
24+
25+
print "==============\n";
26+
27+
$names = array_map(function($name) {
28+
return strtoupper($name);
29+
}, $names);
30+
31+
print "==============\n";
32+
33+
$names = [
34+
"reza",
35+
"hamid",
36+
"ali",
37+
"farzad",
38+
"reza",
39+
"abbas",
40+
"amirreza",
41+
"far"
42+
];
43+
$names = array_map("strtoupper", $names);
44+
print_r($names);
45+
46+
exit();
47+
48+
print "==============\n";
49+
50+
$numbers = [
51+
1,
52+
2,
53+
3,
54+
4,
55+
5,
56+
6,
57+
7,
58+
8,
59+
9,
60+
10
61+
];
62+
$multiply = function($number) {
63+
return $number * 10;
64+
};
65+
$a = array_map($multiply, $numbers);
66+
print_r($a);
67+
68+
$a = [];
69+
foreach ($numbers as $i => $number) {
70+
$a[$i] = $multiply($number);
71+
}

‎call_user_func.php‎

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
// call_user_func
3+
function my_function() {
4+
echo "Hello World!";
5+
}
6+
call_user_func("my_function");
7+
8+
//////////////////
9+
function my_function2() {
10+
echo "22222";
11+
}
12+
function my_function4() {
13+
echo "44444";
14+
}
15+
function my_function6() {
16+
echo "66666";
17+
}
18+
19+
for ($i=2; $i<=6; $i+=2) {
20+
call_user_func("my_function$i");
21+
}
22+
23+
24+
$fn_name = "my_function4";
25+
$fn_name();
26+
27+
$i = 6;
28+
$fn_name = "my_function$i";
29+
$fn_name();
30+
31+
my_function4();
32+
33+
// Error: my_function$i();
34+
35+
for ($i=1; $i<=100; $i++) {
36+
${"age$i"} = $i * 100;
37+
}
38+
print $age1."\n";
39+
print $age2."\n";
40+
print $age3."\n";
41+
42+
// ${"my_function2"}();
43+
print "\n----------------\n";
44+
"my_function2"();
45+
print "\n";
46+
("my_function2")();

‎function.php‎

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php
2+
// function
3+
function my_function() {
4+
echo "Hello World!";
5+
}
6+
my_function();
7+
8+
// call back function
9+
$second = function() {
10+
echo "Hello World!";
11+
};
12+
$second();
13+
14+
$sum = function($a, $b) {
15+
return $a + $b;
16+
};
17+
echo $sum(1, 2);
18+
19+
echo "===================\n";
20+
21+
function my($number1, $number2, $fn) {
22+
$result = $fn($number1, $number2);
23+
$result = $result * 10;
24+
echo $result . "\n";
25+
}
26+
27+
my(1, 2, function($a, $b) {
28+
return $a + $b;
29+
});
30+
31+
my(1, 2, function($a, $b) {
32+
return $a * $b;
33+
});
34+
35+
my(1, 2, function($a, $b) {
36+
return $a - $b;
37+
});
38+
39+
my(1, 2, function($a, $b) {
40+
41+
});
42+
43+
// Run it!
44+
// function() {
45+
// print "Hey!\n";
46+
// }();
47+
48+
(function() {
49+
print "Hey!\n";
50+
})();
51+
52+
(function($name) {
53+
print "Hey $name!\n";
54+
})("Reza");
55+
56+
$sayHey = function($name) {
57+
print "Hey $name!\n";
58+
};
59+
$sayHey("Rey");
60+
unset($sayHey);

‎strtr.php‎

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
<?php
2+
// strtr
3+
$str = "Hello World!";
4+
print strlen($str) . "\n";
5+
6+
7+
// string(12) "****o Wor*d!"
8+
// string(12) "*ello World!"
9+
// string(12) "Hello World!"
10+
// string(12) "***** W*r*d!"
11+
// string(12) "Hello World!"
12+
// string(12) "Hello World!"
13+
var_dump( strtr($str, "Hello", "****") );
14+
var_dump( strtr($str, "Hello", "*") );
15+
var_dump( strtr($str, "Hello", "") );
16+
var_dump( strtr($str, "Hello", "*********") );
17+
var_dump( strtr($str, "Hello", "Hello") );
18+
var_dump( strtr($str, "Hello", "Hello World!") );
19+
20+
21+
// string(12) "Hello Worl*!"
22+
// string(12) "Hello Worl*!"
23+
var_dump( strtr($str, "abcde", "****") );
24+
var_dump( strtr($str, "abcd", "*****") );
25+
26+
27+
// string(12) "****o Wor*d!"
28+
var_dump( strtr($str, "Hello", "**e*") );
29+
// string(12) "**eeo Wored!"
30+
var_dump( strtr($str, "Hello", "***e") );
31+
32+
// $action = [
33+
// "a" => "!",
34+
// "b" => "@",
35+
// "c" => "#"
36+
// ];
37+
// var_dump( strtr($str, $action) );
38+
39+
$pairs = [
40+
"h" => "-",
41+
"hello" => "hi",
42+
"hi" => "hello"
43+
];
44+
// hello all, I said hi
45+
// hello all, I said hi
46+
// hello all, I said hi -
47+
// hello all, I said hi - -o
48+
echo strtr("hi all, I said hello h ho", $pairs);
49+
// The two modes of behavior are substantially different.
50+
// With three arguments, strtr() will replace bytes; with two,
51+
// it may replace longer substrings.
52+
53+
print "\n";
54+
55+
$pairs = [
56+
"h" => "-",
57+
"hilow" => "SSSSS",
58+
"hi" => "hello"
59+
];
60+
echo strtr("hi all, I said hello h ho", $pairs);
61+
// hello all, I said -ello - -o
62+
print "\n";
63+
64+
echo strtr("hi all, I said hilow hiloX h ho", $pairs);
65+
// hello all, I said SSSSS - -o
66+
67+
print "\n===================\n";
68+
69+
$dict = [
70+
"hello" => "سلام",
71+
"bye" => "خداحافظ",
72+
"how are you" => "چطوری؟",
73+
"hey" => "هوی!"
74+
];
75+
echo strtr("hello bye how are you hey", $dict);
76+
// سلام خداحافظ چطوری؟ هوی!

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /