This question already has answers here:
How can I measure the speed of code written in PHP? [closed]
(10 answers)
Closed 1 year ago.
When we have to return only one value parsed by sscanf / fscanf, should we assign a list of one variable or use optional assigned values?
E.G.
list($number) = fscanf($handle, "%d\n")
or
fscanf($handle, "%d\n", $number)
Is there any difference in execution speed of these expressions?
Just benchmark your two ways with a script like this:
<?php
function micro_time() {
$temp = explode(" ", microtime());
return bcadd($temp[0], $temp[1], 6);
}
$time_start = micro_time();
for ($i=0; $i<100; $i++) {
// the code you want to benchmark
}
$time_stop = micro_time();
$time_overall = bcsub($time_stop, $time_start, 6);
echo "Execution time - $time_overall Seconds";
?>
Related
This question already has answers here:
Using braces with dynamic variable names in PHP
(9 answers)
Closed 3 months ago.
I have variables like $start1,$start2...$start($no_col). How can I show all the variables with echo in php? in the code below it doesn't work. $no_col can change from 1 to 10. it is not fixed! I want the result show me all $start1,$start2... $start($no_col) values. All the $start1 ..$start10 variable contain date like 2022-12-10;
for ($i=1; $i <=$no_col ; $i++) {
echo $start.${$i};
The result will be like this:
2022-03-10 2022-09-06 ...
You can use get_defined_vars function for get all variables, and after that show only what you need:
<?php
$start1 = 10;
$start2 = 20;
$start3 = 30;
$start4 = 40;
$start5 = 50;
//get all defined vars
$vars = get_defined_vars();
foreach($vars as $var=>$val) {
if (substr($var, 0, 5) == 'start') {
printf('$%s = %s '. PHP_EOL, $var, $val);
}
}
PHPize - online editor
This question already has answers here:
Break long string into pieces php
(5 answers)
Closed 3 years ago.
I want to make a program which splits a long number into pieces of 13 digited number such that I can loop for every 13 digits just using php.
$number = 012345678901230123456789123
Should output
0123456789123
0123456789123
And it should be for any large number having the number of digit multiple of 13.It looks about looping and algorithm but I want to make it as short as possible and I have doubts on how to do it. So I am just asking about the main concept.
The most dynamic solution is probably to use array_functions on the string.
So str_split to make it array then chunk it in size 13 and implode the arrays.
$number = "012345678901230123456789123";
$arr = array_chunk(str_split($number), 13);
foreach($arr as &$val){
$val = implode($val);
}
https://3v4l.org/LsNFt
You can create a function where you can use your string and size as parameter and return an array of strings of the desired length:
function splitString($str, $packetSize) {
$output = [];
$size = strlen($str);
for ($i = 0; $i < $size; $i += $packetSize) {
if ($i + $packetSize < $size) {
$output[]= substr($str, $i, $packetSize);
} else {
$output[]=substr($str, $i);
}
}
return $output;
}
This question already has answers here:
Generating UNIQUE Random Numbers within a range
(14 answers)
Closed 7 years ago.
I am trying to find a solution in PHP that can generate three random numbers.
At the moment I have this that generates a random number that is different from $randNum;
The numbers need to be different from each other and also different from the variable $randNum
Thank you
$wrong = $randNum;
while ($wrong == $randNum) {
$wrong = rand(0,$max - 1);
}
<?php
$numbers = [];
for($i = 0; $i < 10; $i++){
$number = rand (1,15);
while (in_array($number, $numbers)){
$number = rand (1,15);
}
$numbers[] = $number;
}
echo '<pre>';
print_r($numbers);
echo '</pre>';
This function generates unique 10 random numbers, range from 1-15 you can easy change this script to your needs.
This question already has answers here:
make string of N characters
(5 answers)
Closed 10 years ago.
We have
$symb="_";
$num=10;
We want $ten_symbs to be exactly "__________"; // ten symbols "_".
Whats the fastest and/or the best way to assign ten "_" to $ten_symbs?
str_repeat():
$symbol = '_';
$num = 10;
echo str_repeat($symbol, $num);
You can use str_repeat as:
$ten_symbs = str_repeat($symb, $num);
You can also do:
$ten_symbs = str_pad('',$num,$symb);
But the fist option is cleaner.
look for str_repeat() here: http://www.php.net/manual/de/function.str-repeat.php
$ten_symbs = '';
for($i=0;$i<$num;$i++) {
$ten_symbs .= $symb;
}
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Generate all combinations of arbitrary alphabet up to arbitrary length
I'm trying to make write all possible words of 10 letters(zzzzzzzzzz) in php. How can I do that? it will look like that : http://i.imgur.com/sgUnL.png
I tried some ways to it but they are only making 10 letters randomly not increasing from 1 letter. By the way execution time and how it's big is not problem. i just need to algorithm for it, if somebody show it with code it'll be more helpful of course..
function words($length, $prefix='') {
if ($length == 0) return;
foreach(range('a', 'z') as $letter) {
echo $prefix . $letter, "\n";
words($length-1, $prefix . $letter);
}
}
Usage:
words(10);
Try it here: http://codepad.org/zdTGLtjY (with words up to 3 letters)
Version 1:
for($s = 'a'; $s <= 'zzzzzzzzzz'; print $s++.PHP_EOL);
as noted by Paul in comments below, this will only go to zzzzzzzzyz. A bit slower (if anyone cares) but correct version would be:
//modified to include arnaud576875's method of checking exit condition
for($s = 'a'; !isset($s[10]); print $s++.PHP_EOL);
<?php
function makeWord($length, $prefix='')
{
if ($length <= 0)
{
echo $prefix . "\n";
return;
}
foreach(range('a', 'z') as $letter)
{
makeWord($length - 1, $prefix . $letter);
}
}
// Use the function to write the words.
$minSize = 1;
$maxSize = 3;
for ($i = $minSize; $i <= $maxSize; $i++)
{
makeWord($i);
}
?>