PHP-cycles(example: while & do..while)

in #ua6 years ago

PHP-cycles(example: while & do..while)



temp2.png



<?php

$func=function($im,&$x,&$y,&$x1,&$y1,$color,$h=50){
    $x+=1;$y=pow(sin($x),2)*100;
    imageline($im,$x,$y+$h,$x1,$y1+$h,$color);
    $x1=$x;$y1=$y;    
    $y= ($y>40)?$y/10:$y*10; 
};

$w = 400;$h = 400;
$im=imagecreatetruecolor($w,$h);
imageresolution($im,1150);
$white= imagecolorallocate($im,255,255,255);
$black= imagecolorallocate($im,0,0,0);
imagefill($im,0,0,$white);

$x=1;$y=0;$x1=0;$y1=0;$i=0;
while($i<=$w){$func($im,$x,$y,$x1,$y1,$black);$i++;}echo "{$i}".PHP_EOL;
$x=1;$y=0;$x1=0;$y1=0;$i=0;
do{$func($im,$x,$y,$x1,$y1,$black,200);$i++;}while($i<=$w);echo "{$i}";

imagepng($im, __DIR__ ."/temp2.png");
imagedestroy($im);
while & do..whilewhile & do..while
In the above example, two constructs are used while and do..while. They perform the same action, but checking the logic at each step occurs in the case of while before executing the code inside the loop block, and at do..while o after executing the code inside the block. У вище вказаному прикладі використані дві конструкції while та do..while. Виконують вони однакову дію , але перевірка логічного вирозу на кожному кроці відбувається у випадку з while перед виконанням коду в середені блоку циклу, а у do..while о після виконання коду у середені блоку.
$x=1;$y=0;$x1=0;$y1=0;$i=0;
while($i<=$w){$func($im,$x,$y,$x1,$y1,$black);$i++;}
$x=1;$y=0;$x1=0;$y1=0;$i=0;
do{$func($im,$x,$y,$x1,$y1,$black,200);$i++;}
As you can see, this code, as in the previous article, performs the visualization of the data in the picture. With the help of a loop, logical tracking takes place or the variable x of the picture width, if so, the cycle ends. is useful where it is necessary to carry out a certain action as many times as much as the logical expression is true.. Як бачите цей код як і у попередній статті виконує візуалізацію данних на картинці . За допомогою циклу відбуввається логічне відстеження чи досягнула змінна х довжини полотна , якщо так тоі завершується цикл.Цикл while корисний там де необхідно проводити певно дію стільки разів скільки логічний вираз є істенним .
<?php
...
do {
 if (file_exists(__DIR__."/close.txt"))break;
//some code
}while(true);
...
A simple construct above with a no end loop allows you to make your code no longer a one-time link package, but a complete self-contained application that will work at the command line, for example, will not load your web application with calculations. You need to use careful looping of lifelong cycles , its can damage your pc, but if you correctly process the data and correctly allocate memory, l you can close the application under a certain condition and everything will be ok. As shown in the example above, the "break" construct with condition if completes the perpetual loop when the condition is fulfilled. Проста конструкція вище з зацикленим вічним true дозволяє зробити з вашого коду уже не одноразовий пакет для посилання, а повноціннеи самодостатній застосунок який буде працювати звісно у командній строці , але для прикладу не завантажуватиме ваш веб застосунок обчисленнями.З вічними циклами потрібно користуватись уважно, оскільки можете зашкодити вашому пк, але якщо ви правильно опрацьовуєте дані та правильно виділяєте память , та всеж даєте змогу закриватись застосунку при певній умові тоді все буде ок. Як вказано вище у прикладі конструкція "break" з умовою if завершуть вічний цикл при виконанні умови.

Цикли невідємна екосистеми програмування, якою конструкцією ви би користувались, чи яку би мову програмування використовували , цикли завжди допоможуть вам зменшити код ваших программ . Дякую за вашу вагу підписуйтесь , ставте лайк , коментуйте!!!

Previosly / Раніше :


PHP-cycles(example: for & foreach)
PHP - cycles
Step by step hallo world !!!



FOLLOW ! PRESS UP ! COMMENT!


Be happy, and smile for more!!!!!!





Sort:  

Отличный рисунок получился. Для меня это уровня магии.

This post has received a 3.13 % upvote from @drotto thanks to: @mandarin2016.

Congratulations! This post has been upvoted from the communal account, @minnowsupport, by mandarin2016 from the Minnow Support Project. It's a witness project run by aggroed, ausbitbank, teamsteem, someguy123, neoxian, followbtcnews, and netuoso. The goal is to help Steemit grow by supporting Minnows. Please find us at the Peace, Abundance, and Liberty Network (PALnet) Discord Channel. It's a completely public and open space to all members of the Steemit community who voluntarily choose to be there.

If you would like to delegate to the Minnow Support Project you can do so by clicking on the following links: 50SP, 100SP, 250SP, 500SP, 1000SP, 5000SP.
Be sure to leave at least 50SP undelegated on your account.

Coin Marketplace

STEEM 0.27
TRX 0.13
JST 0.032
BTC 62668.08
ETH 2908.37
USDT 1.00
SBD 3.58