Php fixed number length

How to reduce number length php

No idea why I do it that way, but here is what: Replace the properties with local variables, like Same would work for the conditions however looking closer to the conditions it becomes clear that you have two states only for each variable, so this is actually one condition per variable only (see Type Juggling ) which results in or . I am taking $html as variable and concatenating the table attributes in that and at last I will echo the variable and it will provide same result.

Reduce a number to single digit by adding its digits recursively

How to reduce a number to single digit by adding its individual digits recursively :

Example 2 : $n = 444444 >> 24 >> 6

Example 3 : $n = 8888888888888885 >> 125 >> 8;

then get equal to at last we want to get single digit.

You can use array_sum and str_split into a while loop until the final value of $n has the length equal to 1.

$n = 4444; while (strlen($n) > 1) < $n = array_sum(str_split($n)); >var_dump($n); 

Without array_sum and str_split you can use something like:

$n = '4444'; while (strlen($n) > 1) < $s = 0; for ($i = 0; $i < strlen($n); $i++) < $s += $n[$i]; >$n = (string) $s; > var_dump($n); 

You can calculate this in a much more simple and elegant way, let me try to explain. For example if you have the number 53, you can divide it by 9 and it’s remainder will be it’s reduced number. Don’t ask me how I figured this out, I was just tinkering with numbers. So what you can do is use modulus, (53 % 9 = 8!) (517 % 9 = 4!). Perfect right? Almost, if the number is a multiple of 9 like 45 for example if you “modulus” it by 9 you will receive it’s remaineder which is 0 and we would expect 9 because 45 reduced to a single digit is 9. So you can just make a quick and easy else if statement checking for an output of 0, and if it’s 0 just return 9. Done! Whatever number you out in from 1 to infinty it will reduce it perfectly. Hope this helps 🙂

Читайте также:  Complex комплексное число python

Php — Reduce a number to single digit by adding its digits, You can calculate this in a much more simple and elegant way, let me try to explain. For example if you have the number 53, you can divide it by 9 and it’s remainder will be it’s reduced number. Don’t ask me how I figured this out, I was just tinkering with numbers. So what you can do is use modulus, (53 % 9 = 8!) (517 % 9 = 4!). …

How to reduce number of «echo» in PHP?

I am having problem in reducing number of «echo» in following code:

Please provide a solution.

 echo "
"."
". ""; foreach ($result as $values) < echo ""; foreach ($values as $data) < echo ""; > echo ""; > echo "
". $data. "
";

This is the first step towards realising that you need a separate templating layer. There are plenty of them available (Twig, Blade, Smarty), but PHP itself can provide a good example:

The alternative control syntax allows you to work without littering the template with < and >everywhere, which in my opinion makes things a lot more readable. The short echo tag

It’s ultimately a matter of opinion, but this is a clean approach.

It’s better to keep logic and display separate, so you could make an $output variable and echo it once at the end!


"; foreach ($result as $values) < $output .= ""; foreach ($values as $data) < $output .=""; > $output .=""; > $output .="
". $data. "
"; echo $output;

You can take a variable instead of echoing on each stem and at last echo that variable. For Eg. I am taking $html as variable and concatenating the table attributes in that and at last I will echo the variable and it will provide same result.

$html = "
"."
". ""; foreach ($result as $values) < $html.=""; foreach ($values as $data) < $html.= ""; > $html.= ""; > $html.= "
". $data. "
"; // echo full table together in one variable. echo $html;
$string = "

"; foreach ($result as $values) < $string .= ""; foreach ($values as $data) < $string .= ""; > $string .= ""; > $string .= "
". $data. "
"; echo $string;

How to reduce/compress the lenght of short strings, My client wants to store more than 60 characters length text but I can not use more than 60 characters to store this data, so I am looking for a …

How to reduce the number of IF statements?

I have many IF sentences that each start a function.
Is there an obvious way to write this code much simpler?
Every IF starts different function, but it still looks like an overkill.

 if ($this->machine == '' AND $this->date_from == '' AND $this->date_to == '' AND $this->date_like == '' AND $this->article_or_tool == '') < $this->AllTime(); > if ($this->machine <> 0 AND $this->date_from == '' AND $this->date_to == '' AND $this->date_like == '' AND $this->article_or_tool == '') < $this->ByMachine(); > if ($this->machine == '' AND $this->date_from <> 0 AND $this->date_to <> 0 AND $this->date_like == '' AND $this->article_or_tool == '') < $this->ByDate(); > if ($this->machine <> 0 AND $this->date_from <> 0 AND $this->date_to <> 0 AND $this->date_like == '' AND $this->article_or_tool == '') < $this->ByMachineByDate(); > if ($this->machine == '' AND $this->date_from == '' AND $this->date_to == '' AND $this->date_like <> 0 AND $this->article_or_tool == '') < $this->ByDateLike(); > if ($this->machine <> 0 AND $this->date_from == '' AND $this->date_to == '' AND $this->date_like <> 0 AND $this->article_or_tool == '') < $this->ByMachineByDateLike(); > if ($this->machine == '' AND $this->date_from == '' AND $this->date_to == '' AND $this->date_like == '' AND $this->article_or_tool <> 0) < $this->ByArticle(); > if ($this->machine <> 0 AND $this->date_from == '' AND $this->date_to == '' AND $this->date_like == '' AND $this->article_or_tool <> 0) < $this->ByMachineByArticle(); > if ($this->machine == '' AND $this->date_from <> 0 AND $this->date_to <> 0 AND $this->date_like == '' AND $this->article_or_tool <> 0) < $this->ByDateByArticle(); > if ($this->machine == '' AND $this->date_from == '' AND $this->date_to == '' AND $this->date_like <> 0 AND $this->article_or_tool <> 0) < $this->ByDateLikeByArticle(); > if ($this->machine <> 0 AND $this->date_from <> 0 AND $this->date_to <> 0 AND $this->date_like == '' AND $this->article_or_tool <> 0) < $this->ByMachineByDateByArticle(); > if ($this->machine <> 0 AND $this->date_from == '' AND $this->date_to == '' AND $this->date_like <> 0 AND $this->article_or_tool <> 0) < $this->ByMachineByDateLikeByArticle(); > 

SOLUTION
Here is my code after refactoring it:

function MethodPicker() < $machine = $this->machine <> 0; $date_from = $this->date_from <> 0; $date_to = $this->date_to <> 0; $date_like = $this->date_like <> 0; $article_or_tool = $this->article_or_tool <> 0; $decision = array($machine, $date_from, $date_to, $date_like, $article_or_tool); $decisions = array( 'AllTime' => array(false, false, false, false, false ), 'ByMachine' => array(true, false, false, false, false ), 'ByDate' => array(false, true, true, false, false ), 'ByMachineByDate' => array(true, true, true, false, false ), 'ByDateLike' => array(false, false, false, true, false ), 'ByMachineByDateLike' => array(true, false, false, true, false ), 'ByArticle' => array(false, false, false, false, true ), 'ByMachineByArticle' => array(true, false, false, false, true ), 'ByDateByArticle' => array(false, true, true, false, true ), 'ByDateLikeByArticle' => array(false, false, false, true, true ), 'ByMachineByDateByArticle' => array(true, true, true, false, true ), 'ByMachineByDateLikeByArticle' => array(true, false, false, true, true ), ); $method = array_keys($decisions, $decision, true); $method && list($method) = $method; $method && $this->$method(); > 

First of all I would do some standard refactorings. No idea why I do it that way, but here is what:

    Replace the properties with local variables, like

$machine = $this->machine == '' || $this->machine == 0; 

( Credits go to martinstoeckli for the correct condition )

This would be a start. The if clauses until now would have already changed and would be more compact. However, why stop here? There is a decision that is current:

$decision = [$machine, $date_from, $date_to, $date_like, $article_or_tool]; 

And there is a set of decisions to choose from:

$decisions = [ 'AllTime' => [true, true, true, true, true], . ]; 

So all that needs to be done is to find the decision and execute the method:

$method = array_keys($decisions, $decision, true); $method && $this->$method(); 

The if block has been turned into a matrix. The function has been mapped to one state of it.

You loose the names on the fields, however, you could solve that with a comment:

 $decisions = [ // machine from to like article 'AllTime' => [true , true, true, true, true], . ]; 
$machine = $this->machine == '' || $this->machine == 0; . # 4 more times $decision = [$machine, $date_from, $date_to, $date_like, $article_or_tool]; $decisions = [ 'AllTime' => [true, true, true, true, true], . # 11 more times ]; $method = array_keys($decisions, $decision, true); $method && $this->$method(); 

If the class this is in represent a value object, I suggest you move the decisions into a type of it’s own and then just use that decision type as a single method object. Will enable you later on to do different sets of decisions more easily.

Maybe you really need all this decisions, but you could make it a bit easier to read. Instead of writing many times $this->machine == » inside the if statement, you could set this value to a meaningful variable first.

$machineIsEmpty = $this->machine == '' || $this->machine == 0; $dateFromIsEmpty = $this->date_from == '' || $this->machine == 0; . if ($machineIsEmpty && $dateFromIsEmpty && $dateToIsEmpty && $dateLikeIsEmpty && $articleOrToolIsEmpty) < $this->AllTime(); > else if (!$machineIsEmpty && $dateFromIsEmpty && $dateToIsEmpty && $dateLikeIsEmpty && $articleOrToolIsEmpty) < $this->ByMachine(); > . 

In this example i assume two things: first i suspect that you want to handle both the values » and 0 as not set . I’m not sure about this, because there is no case where you did anything with the value 0 .

Second i assume that if one function was called, you do not want to call further functions, so i added an else before the next if.

Nesting the if statements would in my opinion make the code more difficult to read, because you have to remember in which level of if statement you currently are.

An alternative approach would be to use a decision-matrix. You can write an array holding all possible combinations, and each combination knows the function name.

$myObject = new TestClass(); $myObject->DoAction($machineIsSet, $dateFromIsSet, $dateToIsSet, $dateLikeIsSet, $articleToolIsSet); class TestClass < private $actionMatrix = array( // machine, dateFrom, dateTo, dateLike, articleOrTool, action array(false, false, false, false, false, 'AllTime'), array(true, false, false, false, false, 'ByMachine') ); public function DoAction($machine, $dateFrom, $dateTo, $dateLike, $articleOrTool) < foreach($this->actionMatrix as $action) < if (($action[0] == $machine) && ($action[1] == $dateFrom) && ($action[2] == $dateTo) && ($action[3] == $dateLike) && ($action[4] == $articleOrToolLike)) < $functionName = $action[5]; $this->$functionName(); // call the function break; > > // no action found, maybe we want some error handling here? > public function AllTime() < echo('AllTime'); >public function ByMachine() < echo('ByMachine'); >> 

A lot of it is repetition, so you could nest the conditions to remove redundancy, for example:

if ($this->machine == '') < // do everything requiring empty 'machine' string // remove condition from all subsequent ifs in this context >else if ($this-> machine <> 0)

I don’t have the time or the inclination to go through all of that code and actually do this for you, but that’s an idea that should provide enough information for you to implement as an exercise for the reader. (:

Sometimes it is not possible if your conditions are different, but in this case you can start from grouping your if() s, as many share the same condition. For example instead of having tons of

you could group them all together:

And then proceed with next «level» of conditions. While this may NOT reduce total number of if() s, your code will be much more readable than it is now.

Php — How to reduce the length of very long strings?, Is it possible to strip the very long line of *s in the above example down to a manageable length? The extra complication is that is not always *s …

Источник

Оцените статью