Хабр Курсы для всех
РЕКЛАМА
Практикум, Хекслет, SkyPro, авторские курсы — собрали всех и попросили скидки. Осталось выбрать!
пространные вопросы типа чем отличается php 5.3 от php 5.2.
какую бы вы предложили версию для реализации %problemName% и почему?.
<?php
$arr = array(‘h’, ‘e’, ‘l’, ‘l’, ‘o’);
$reversed = array();
for ($i=0; $i<count($arr); $i++) array_unshift($reversed, $arr[$i]);
for ($i=0; $i<count($reversed); $i++) echo “$reversed[$i]”;
?>for ($i=count($arr)-1; $i<=0; $i++) { $reversed[] = $arr[$i]; }
<?php
define('NUM', 1000000);
$start = microtime(true);
for ($i = 0; $i < NUM; $i++);
$for_time = microtime(true) - $start;
$some_str = "Hello world!";
echo "For loop time: " . round($for_time, 3) . " sec\n";
$start = microtime(true);
for ($i = 0; $i < NUM; $i++);
echo "Empty stmt time (should be 0): " . round(microtime(true) - $start - $for_time, 3) . " sec\n";;
$start = microtime(true);
for ($i = 0; $i < NUM; $i++) {
"hello world $for_time $i $for_time $i $for_time $i $some_str $some_str";
}
echo "Double quotes: " . round(microtime(true) - $start - $for_time, 3) . " sec\n";
$start = microtime(true);
for ($i = 0; $i < NUM; $i++) {
'hello world ' . $for_time . ' ' . $i . ' ' . $for_time .' ' . $i . ' ' . $for_time . ' ' . $i . ' ' . $some_str . ' ' . $some_str;
}
echo "Single quotes: " . round(microtime(true) - $start - $for_time, 3) . " sec\n";
$ php bench.php
For loop time: 0.097 sec
Empty stmt time (should be 0): -0.005 sec
Double quotes: 2.183 sec
Single quotes: 2.891 sec
что это более неверно, причём уже довольно давно.
for ($i = count($arr); $i--;} {
$reversed[] = $arr[$i];
}
$arr = array(‘h’, ‘e’, ‘l’, ‘l’, ‘o’);
for ($i = 0; $i < floor(count($arr)/2); $i++)
{
$tmp = $arr[$i];
$arr[$i] = $arr[count($arr)-$i-1];
$arr[count($arr)-$i-1] = $tmp;
}
$source = array( 50 , 40 , 30 , 20, 10);
$pieces = count($array)-1;
$reversed = array();
while($pieces >= 0) {
$reversed[] = $source[$pieces--];
}
Array (
[0] => 10
[1] => 20
[2] => 30
[3] => 40
[4] => 50
) $source = array( 50 , 40 , 30 , 20, 10 );
$rev = array();
while ( count ( $source ) ) $rev[] = array_pop( $source );
<?php
$iterations = 100001;
$arr = array('h', 'e', 'l', 'l', 'o');
$t1 = microtime(true);
for ($i = 0; $i < $iterations; $i++) {
$arr = reverse1($arr);
}
$t1 = microtime(true) - $t1;
var_dump($arr);
var_dump($t1);
$arr = array('h', 'e', 'l', 'l', 'o');
$t2 = microtime(true);
for ($i = 0; $i < $iterations; $i++) {
$arr = reverse2($arr);
}
$t2 = microtime(true) - $t2;
var_dump($arr);
var_dump($t2);
function reverse1($arr) {
for ($i = 0; $i < floor(count($arr)/2); $i++)
{
$tmp = $arr[$i];
$arr[$i] = $arr[count($arr)-$i-1];
$arr[count($arr)-$i-1] = $tmp;
}
return $arr;
}
function reverse2($arr) {
$c = count($arr);
for ($i = 0; $i < floor($c/2); $i++)
{
$tmp = $arr[$i];
$arr[$i] = $arr[$c-$i-1];
$arr[$c-$i-1] = $tmp;
}
return $arr;
}
array
0 => string 'o' (length=1)
1 => string 'l' (length=1)
2 => string 'l' (length=1)
3 => string 'e' (length=1)
4 => string 'h' (length=1)
float 2.8682391643524
array
0 => string 'o' (length=1)
1 => string 'l' (length=1)
2 => string 'l' (length=1)
3 => string 'e' (length=1)
4 => string 'h' (length=1)
float 1.7087500095367
<?php
$iterations = 100001;
$t1 = microtime(true);
for ($i = 0; $i < $iterations; $i++) {
$a = 1;
}
$t1 = microtime(true) - $t1;
var_dump($t1);
float 0.038902044296265
for ($i = 0; $i < $count($arr); $i++) {
$a = $arr[$i];
}
$iterations = count($arr);
for ($i = 0; $i < $iterations; $i++) {
$a = $arr[$i];
}
<?php
$array = range(0, 100000);
$t1 = microtime(true);
for ($i = 0; $i < count($array); $i++) {
$a = $array[$i];
}
$t1 = microtime(true) - $t1;
var_dump($t1);
$t1 = microtime(true);
$c = count($array);
for ($i = 0; $i < $c; $i++) {
$a = $array[$i];
}
$t1 = microtime(true) - $t1;
var_dump($t1);
float 0.24822592735291
float 0.046469926834106
$arr = range(0, 100500);
$start = xdebug_time_index();
// code...
echo xdebug_time_index() - $start . ' ' . memory_get_peak_usage();
for ($i = 0; $i < floor(count($arr)/2); $i++)
{
$tmp = $arr[$i];
$arr[$i] = $arr[count($arr)-$i-1];
$arr[count($arr)-$i-1] = $tmp;
}
$rev = array();
while ( count( $arr ) ) $rev[] = array_pop( $arr );
$count = count($arr);
for ($i = 0; $i < floor($count/2); $i++)
{
$tmp = $arr[$i];
$arr[$i] = $arr[$count-$i-1];
$arr[$count-$i-1] = $tmp;
}
$count = count($arr);
for ($i = intval($count / 2) - 1; $i >= 0; $i--)
{
$tmp = $arr[$i];
$arr[$i] = $arr[$count-$i-1];
$arr[$count-$i-1] = $tmp;
}
$pieces = count($arr)-1;
$reversed = array();
while($pieces >= 0) {
$reversed[] = $arr[$pieces--];
}
for ($l = 0, $r = count($arr) - 1; $l < $r; ++$l, --$r)
{
$t = $arr[$l]; $arr[$l] = $arr[$r]; $arr[$r] = $t;
}
$arr = array_reverse($arr);
<?php
function boo($num) {
switch($num) {
case “2”:
echo “two!”;
break;
case “3”:
echo “three!”;
break;
default:
echo “shnyaga!”;
break;
}
}
boo(2);
?>$i = 5;
$i+= $i++ + ++$i;// mixing ++ and + produces undefined behavior
$a = 1;
echo ++$a + $a++; // may print 4 or 5что выведет на экран $i++ -$i-- * ++$j39. Есть массив a = array(тут много элементов). Проходим по массиву циклом for (i=0; i<=count(a); i++). Можно ли как-нибудь ускорить цикл?
Да.
1) Вынести count(a) в отдельную переменную;
2) Считать массив с конца циклом for (i=count(a); i>=0; i--).
<?php
$counter=0;
function fibonacci($n)
{
global $counter;
$counter++;
if ($n < 3) {
return 1;
} else {
return fibonacci($n-1) + fibonacci($n-2);
}
}
for ($n = 1; $n <= 40; $n++) {
$counter=0;
echo(fibonacci($n) . " ");
print " Операций: ".$counter.'
';
}
196418 Операций: 392835
317811 Операций: 635621
514229 Операций: 1028457
832040 Операций: 1664079
1346269 Операций: 2692537
<?php
$strings = array();
for ($i = 1; $i <= 100; $i++) {
$strings[] = str_repeat('а', 1000 * $i);
}
foreach ($strings as $str) {
$start = microtime(true);
iconv("UTF-16LE", "UTF-8", strrev(iconv("UTF-8", "UTF-16BE", $str)));
iconv("UTF-16LE", "UTF-8", strrev(iconv("UTF-8", "UTF-16BE", $str)));
iconv("UTF-16LE", "UTF-8", strrev(iconv("UTF-8", "UTF-16BE", $str)));
iconv("UTF-16LE", "UTF-8", strrev(iconv("UTF-8", "UTF-16BE", $str)));
iconv("UTF-16LE", "UTF-8", strrev(iconv("UTF-8", "UTF-16BE", $str)));
iconv("UTF-16LE", "UTF-8", strrev(iconv("UTF-8", "UTF-16BE", $str)));
iconv("UTF-16LE", "UTF-8", strrev(iconv("UTF-8", "UTF-16BE", $str)));
iconv("UTF-16LE", "UTF-8", strrev(iconv("UTF-8", "UTF-16BE", $str)));
iconv("UTF-16LE", "UTF-8", strrev(iconv("UTF-8", "UTF-16BE", $str)));
iconv("UTF-16LE", "UTF-8", strrev(iconv("UTF-8", "UTF-16BE", $str)));
iconv("UTF-16LE", "UTF-8", strrev(iconv("UTF-8", "UTF-16BE", $str)));
iconv("UTF-16LE", "UTF-8", strrev(iconv("UTF-8", "UTF-16BE", $str)));
iconv("UTF-16LE", "UTF-8", strrev(iconv("UTF-8", "UTF-16BE", $str)));
iconv("UTF-16LE", "UTF-8", strrev(iconv("UTF-8", "UTF-16BE", $str)));
iconv("UTF-16LE", "UTF-8", strrev(iconv("UTF-8", "UTF-16BE", $str)));
iconv("UTF-16LE", "UTF-8", strrev(iconv("UTF-8", "UTF-16BE", $str)));
iconv("UTF-16LE", "UTF-8", strrev(iconv("UTF-8", "UTF-16BE", $str)));
echo ((microtime(true) - $start) * 1000) . "\n";
}

<?php
$strings = array();
for ($i = 1; $i <= 100; $i++) {
$strings[] = str_repeat('а', 100 * $i);
}
mb_internal_encoding("UTF-8");
function mb_strrev($str) {
$len = mb_strlen($str);
$new_str = '';
for ($i = $len - 1; $i >= 0; $i--) $new_str .= mb_substr($str, $i, 1);
return $new_str;
}
foreach ($strings as $str) {
$start = microtime(true);
mb_strrev($str);
echo ((microtime(true) - $start) * 1000) . "\n";
}

<?php
$str = str_repeat('а', 99) . 'б';
mb_internal_encoding("UTF-8");
function utf8_strrev_joomla($str){
preg_match_all('/./us', $str, $ar);
return join('',array_reverse($ar[0]));
}
function utf8_strrev_mb($str) {
$len = mb_strlen($str);
$new_str = '';
for ($i = $len - 1; $i >= 0; $i--) $new_str .= mb_substr($str, $i, 1);
return $new_str;
}
function utf8_strrev($str) {
return iconv("UTF-16LE", "UTF-8", strrev(iconv("UTF-8", "UTF-16BE", $str)));
}
$start = microtime(true);
for ($i = 0; $i <= 10000; $i++) {
utf8_strrev_joomla($str);
}
echo "Joomla: " . round(microtime(true) - $start, 5) . " sec\n";
$start = microtime(true);
for ($i = 0; $i <= 10000; $i++) {
utf8_strrev_mb($str);
}
echo "Mb: " . round(microtime(true) - $start, 5) . " sec\n";
$start = microtime(true);
for ($i = 0; $i <= 10000; $i++) {
utf8_strrev($str);
}
echo "My: " . round(microtime(true) - $start, 5) . " sec\n";
$start = microtime(true);
for ($i = 0; $i <= 10000; $i++) {
strrev($str);
}
echo "Plain: " . round(microtime(true) - $start, 5) . " sec\n";
$ref = utf8_strrev_joomla($str);
if ($ref != utf8_strrev_mb($str)) echo "Mb is incorrect\n";
if ($ref != utf8_strrev($str)) echo "My is incorrect\n";
if ($ref != strrev($str)) echo "Plain is incorrect\n";
Joomla: 0.06649 sec
Mb: 0.05858 sec
My: 0.02206 sec
Plain: 0.00231 sec
Plain is incorrect
Joomla: 0.5095 sec
Mb: 0.67563 sec
My: 0.04836 sec
Plain: 0.00398 sec
Plain is incorrect
function utf8_strrev_mb2($str) {
return mb_convert_encoding( strrev( mb_convert_encoding($str, 'UTF-16BE', 'UTF-8') ), 'UTF-8', 'UTF-16LE');
}function utf8_strrev_iconv2($str) {
$len = iconv_strlen($str, "UTF-8");
$new_str = '';
for ($i = $len - 1; $i >= 0; $i--) $new_str .= iconv_substr($str, $i, 1, "UTF-8");
return $new_str;
}<?
$test = 'Тест';
$end = strlen($test)-1;
$half = strlen($test)>>1;
$test = iconv('utf-8', 'utf-16le', $test);
for ($i = 0; $i < $half; $i++) {
list($test[$i], $test[$end-$i]) = array($test[$end-$i], $test[$i]);
}
echo iconv('utf-16be', 'utf-8', $test);
function utf8_strrev($str) {
return iconv("UTF-16LE", "UTF-8", sttrev(iconv("UTF-8", "UTF-16BE", $str)));
}
$a = 0;
$b = null;
$c = "0";
echo ($a == $b);
echo ($a == $c);
echo ($a == $b);
$i = 3;
if ($i > 2 || exit()) {
echo 'hello';
}$str = "0";
if ($str) echo 'EMPTY';
else echo 'NOT EMPTY';
if (empty($str)) echo 'EMPTY';
else echo 'NOT EMPTY';
if (strlen($str)) echo 'EMPTY';
else echo 'NOT EMPTY';
$str = "0";
if (!$str) echo 'EMPTY';
else echo 'NOT EMPTY';
if (empty($str)) echo 'EMPTY';
else echo 'NOT EMPTY';
if (!strlen($str)) echo 'EMPTY';
else echo 'NOT EMPTY';
$ cat test.php
<?php
$str = '0';
if (empty($str)) echo "Empty\n";
else echo "Not empty!\n";
$ php test.php
Empty
29. В чём разница между функциями count() и sizeof()?
2. Как в JavaScript вывести на экран число, которое будет меняться, например, каждую секунду?
hey = 1;
function foo() {
document.write(hey);
hey++;
}setInterval(“foo()”, 1000);
$a=0;
if($a=$b)
echo "ку!";
else
echo "кю!";GET и POST – это основные методы передачи данных. Есть еще PUT и DELETE, но ими никто не пользуется.
GET и POST – это основные методы передачи данных. Есть еще PUT и DELETE, но ими никто не пользуется.
PHP. Собеседование в вопросах и ответах