Заметки для начинающих в PHP
45 сообщений
#16 лет назад
В вашем файле функция:
function countAll($arg1)
{
if (func_num_args() == 0)
{
die("You need to specify at least one argument");
}
else
{
$args = func_get_args(); // Returns an array of arguments
// Remove the defined argument from the beginning
array_shift($args);
$count = strlen ($arg1);
foreach ($args as $arg)
{
$count += strlen($arg);
}
}
return $count;
}
Перепишем эту функцию по другому:
function countAll2($arg1)
{
if (func_num_args() == 0)
{
die("You need to specify at least one argument");
}
else
{
$args = func_get_args(); // Returns an array of arguments
$count = strlen(implode('', $args));
}
return $count;
}
strlen считается только один раз. По идее должно быть быстрее.
Давайте проверим:
<?php
function countAll($arg1)
{
if (func_num_args() == 0)
{
die("You need to specify at least one argument");
}
else
{
$args = func_get_args(); // Returns an array of arguments
// Remove the defined argument from the beginning
array_shift($args);
$count = strlen ($arg1);
foreach ($args as $arg)
{
$count += strlen($arg);
}
}
return $count;
}
function countAll2($arg1)
{
if (func_num_args() == 0)
{
die("You need to specify at least one argument");
}
else
{
$args = func_get_args(); // Returns an array of arguments
$count = strlen(implode('', $args));
}
return $count;
}
function timer_start()
{
global $timestart;
$mtime = explode(' ', microtime() );
$mtime = $mtime + $mtime;
$timestart = $mtime;
return true;
}
function timer_stop($display = 0, $precision = 3)
{ //if called like timer_stop(1), will echo $timetotal
global $timestart, $timeend;
$mtime = microtime();
$mtime = explode(' ',$mtime);
$mtime = $mtime + $mtime;
$timeend = $mtime;
$timetotal = $timeend-$timestart;
$r = number_format($timetotal, $precision);
if ( $display )
echo $r;
return $r;
}
timer_start();
for($i=0; $i < 1000000; $i++)
{
countAll("foo", "bar", "baz");
}
timer_stop(1);
echo "\r\n";
timer_start();
for($i=0; $i < 1000000; $i++)
{
countAll2("foo", "bar", "baz");
}
timer_stop(1);
echo "\r\n";
?>
Запускаем:
oleg@debian:/var/www$ php test2.php
9.595
7.483
oleg@debian:/var/www$ php test2.php
10.535
7.863
oleg@debian:/var/www$ php test2.php
10.194
7.780
oleg@debian:/var/www$ php test2.php
10.229
7.773
205 сообщений
#16 лет назад
Забыты очень важные фичи php
<?php
$a = 5; //int
$b = '5string'; //string
$c = 'string5'; //string
echo $a + $b;
echo $b + $a;
echo $a + $c;
echo $c + $a;
<?php
define('CONSTANT', 'value');
var_dump(CONSTANT);
var_dump(UNDEFINED_CONST);
if (UNDEFINED_CONST)
{
echo 'обрабатывается этот участок кода';
}
if (UNDEFINED_CONST === TRUE)
{
//а так не выполниться
}
<?php
class e1it3
{
}
$test = new e1it3;
$test->a = 'string';
//$test->a и $test->A различны для php
var_dump($test->A);
var_dump($test->a);
ps красота: ссылка
205 сообщений
45 сообщений
#16 лет назад
Цитата:
<?php
class e1it3
{
}
$test = new e1it3;
$test->a = 'string';
//$test->a и $test->A различны для php
var_dump($test->A);
var_dump($test->a);
Поставьте error_reporting (E_ALL | E_STRICT);.
1267 сообщений
#16 лет назад
Я не спорю, что там очень многого не хватает, но это исключительно подборка с 1 главы книги