我想得到的总和1 + 2 + ... + 1000000000
,但我在PHP和Node.js得到了有趣的结果.
PHP
$sum = 0;
for($i = 0; $i <= 1000000000 ; $i++) {
$sum += $i;
}
printf("%s", number_format($sum, 0, "", "")); // 500000000067108992
Run Code Online (Sandbox Code Playgroud)
Node.js的
var sum = 0;
for (i = 0; i <= 1000000000; i++) {
sum += i ;
}
console.log(sum); // 500000000067109000
Run Code Online (Sandbox Code Playgroud)
可以使用计算正确的答案
1 + 2 + ... + n = n(n+1)/2
Run Code Online (Sandbox Code Playgroud)
正确答案= 500000000500000000,所以我决定尝试另一种语言.
走
var sum , i int64
for i = 0 …
Run Code Online (Sandbox Code Playgroud) 介绍
如何IP address
从Web应用程序/服务器中阻止大量数据.显然,可以用PHP
任何编程语言轻松完成
$ipList = []; // array list or from database
if (in_array(getIP(), $ipList)) {
// Log IP & Access information
header("https://www.google.com.ng/search?q=fool"); // redirect
exit(); // exit
}
Run Code Online (Sandbox Code Playgroud)
或使用 htaccess
order allow,deny
deny from 123.45.6.7
deny from 012.34.5.
# .... the list continues
allow from all
Run Code Online (Sandbox Code Playgroud)
问题
100k plus individual IPs
不subnets
htaccess
了iptables
100000+ 禁令只是荒谬(可能是错的)愚蠢的想法
order allow,deny
deny from database <-------- Not sure if this is …
Run Code Online (Sandbox Code Playgroud) 这是一个简单的循环
$list = array("A", "B", "C","D");
foreach ($list as $var) {
print(current($list));
}
Run Code Online (Sandbox Code Playgroud)
输出(演示)
BBBB // Output for 5.2.4 - 5.5.0alpha4
BCD // Output for 4.4.1
AAAA // Output for 4.3.0 - 4.4.0, 4.4.2 - 5.2.3
Run Code Online (Sandbox Code Playgroud)
题 :
foreach
我制作的,AAAA
但是在当前的PHP
稳定版本中没有得到它注意*我知道我可以简单地使用print $var
来自PHP DOC
current - 返回数组中的当前元素current()函数只返回内部指针当前指向的数组元素的值.它不会以任何方式移动指针.如果内部指针指向超出元素列表末尾或数组为空,则current()返回FALSE.
更新1 - 新观察
感谢Daniel Figueroa:只需通过包装current
功能,您就可以得到不同的结果
foreach ( $list as $var ) {
print(item($list));
}
function item($list) { …
Run Code Online (Sandbox Code Playgroud) 已经遇到过这么多次,我不知道为什么会这样让我感到好奇.有些班级在宣布之前有效,有些则没有;
例1
$test = new TestClass(); // top of class
class TestClass {
function __construct() {
var_dump(__METHOD__);
}
}
Run Code Online (Sandbox Code Playgroud)
产量
string 'TestClass::__construct' (length=22)
Run Code Online (Sandbox Code Playgroud)
例2
当类扩展另一个类或实现任何接口时
$test = new TestClass(); // top of class
class TestClass implements JsonSerializable {
function __construct() {
var_dump(__METHOD__);
}
public function jsonSerialize() {
return json_encode(rand(1, 10));
}
}
Run Code Online (Sandbox Code Playgroud)
产量
Fatal error: Class 'TestClass' not found
Run Code Online (Sandbox Code Playgroud)
例3
让我们尝试上面的同一个班级,但改变位置
class TestClass implements JsonSerializable {
function __construct() {
var_dump(__METHOD__);
}
public function jsonSerialize() {
return json_encode(rand(1, 10));
}
} …
Run Code Online (Sandbox Code Playgroud) while (true)
vs while (\true)
和most 之间有什么区别重要的是,任何人都可以澄清为什么boolean
会受命名空间影响PHP
?
strpos功能反向
我想找到一种方法,我可以在其中找到反向的字符位置.例如,最后"e"反向开始计数.
从例子
$string="Kelley";
$strposition = strpos($string, 'e');
Run Code Online (Sandbox Code Playgroud)
它会给我一个位置1.
如何让你获得元素key
和value
在一个n
在无环中特定位置的位置阵列.
想像
$postion = 3; // get array at 3rd position
$array = array(
"A" => "Four",
"B" => "twp",
"C" => "three",
"D" => "Four",
"E" => "Five",
"F" => "Four");
$keys = array_keys($array);
$value = array_values($array);
echo implode(array_slice($keys, $postion, 1)), PHP_EOL; // Key at 3rd posstion
echo implode(array_slice($value, $postion, 1)), PHP_EOL; // Value at n position
Run Code Online (Sandbox Code Playgroud)
产量
D
Four
Run Code Online (Sandbox Code Playgroud)
这个方法的问题是
为什么不使用循环
为什么不使用数据库
为什么不用 SplFixedArray
这本来是解决方案,但我跟随自己,因为我没有使用正面的键(我真的这是不公平的PHP部分)
Fatal error: Uncaught exception …
Run Code Online (Sandbox Code Playgroud)PHP
调用父类中的私有方法,而不是调用当前类中的方法 call_user_func
class Car {
public function run() {
return call_user_func(array('Toyota','getName')); // should call toyota
}
private static function getName() {
return 'Car';
}
}
class Toyota extends Car {
public static function getName() {
return 'Toyota';
}
}
$car = new Car();
echo $car->run(); //Car instead of Toyota
$toyota = new Toyota();
echo $toyota->run(); //Car instead of Toyota
Run Code Online (Sandbox Code Playgroud) 上个月,codepad.viper-7.com已关闭,很多stackoverflow的答案都粘贴在那里,包括我的一些代码.当它返回所有丢失的脚本.
要寻找一个免费匿名替代codepad.viper-7.com在那里我可以测试php
最小限制(脚本fopen
,curl
,mb_strlen
等); 在不同的版本.
这里有很多推荐,但很难找到合适的替代品到codepad.viper-7.com
简单的测试脚本
<?php
echo strlen(file_get_contents("http://google.com"));
?>
Run Code Online (Sandbox Code Playgroud)
产量
题:
有没有限制的匿名替代方案?
我有一个PHP数组,看起来像这样:
array (
[0] => array (
[id] => 1
[title] => "Title 1"
[parent_id] => NULL
[depth] => 0
)
[1] => array (
[id] => 2
[title] => "Title 2"
[parent_id] => NULL
[depth] => 0
)
[2] => array (
[id] => 3
[title] => "Title 3"
[parent_id] => 2
[depth] => 1
)
[3] => array (
[id] => 4
[title] => "Title 4"
[parent_id] => 2
[depth] => 1
)
[4] => array (
[id] …
Run Code Online (Sandbox Code Playgroud)