PHP-cycles(example: for & foreach)

in #ua6 years ago (edited)


PHP-cycles(example: for & foreach)




temp1.png



<?php
/**
*  Змінна для зберігання координат 'X' та 'Y' ,масив
*  
* $coord
*/
$coord=[];
for($x=1;$x<399;$x=$x+3){
    $coord[]=['x'=>$x,'y'=>cos($x)*30]; //формуємо координати x та y
}

print_r($coord);

$w = 400;
$h = 200;
$im=imagecreatetruecolor($w,$h);
imageresolution($im,1150);
$white= imagecolorallocate($im,255,255,255);
$black= imagecolorallocate($im,0,0,0);
imagefill($im,0,0,$white);
$t=0;
$xyf=function($xy)use($h){
   return ['x'=> $xy['x'],'y'=>$xy['y']+$h/2];
};

foreach($coord as $k => $xy){// по елементно передаємо дані з масиву 
    $c2=$xyf($xy);$c1=$xyf($coord[$t]);
$t=$k;//

    imageline($im,$c1['x'],$c1['y'],$c2['x'],$c2['y'],$black);
   // imagesetpixel($im, $x,$y,$black);
    
}  

imagepng($im, __DIR__ ."/temp1.png");
imagedestroy($im);
for for
Imagine that you have an array of coordinates, any lengths that are updated, and so on, and you need to convey these coordinates to the image.
In the code I have formed a coordinate array using the FOR construct:
Уявіть що у вас є масив з координат , будь яка довжина , які оновлюються і тд, і вам ці координати треба передати у зоображення .
У коді Я сформував масив з координат за допомогою конструкції FOR:
<?php
for($x=1;$x<399;$x=$x+3){
    $coord[]=['x'=>$x,'y'=>cos($x)*30]; //формуємо координати x та y
}
1) $ x = 1 is the original value of X;
2) $ x <399 - the condition for the ctycle end ,if X is equal to or greater than 399;
3) $ x = $ x + 3 is the incremental step for X;
1) $x=1 - початкове значення X;
2) $x<399 - умова згідно якої завершиться цикл, якщо X буде дорівнювати або більшим за 399;
3) $x=$x+3 - крок з яким збільшується Х на кожному новому кроці;
foreachforeach
When we built an array of coordinates left to pass it into an image, FOREACH's design, the main task of which is to parse arrays by element, is most suitable for this. And it looks like this in the code Коли ми побудували масив координат залишилось передати його у зображення, для цього найраще підійде конструкція FOREACH, головною задачею якої є перебір масивів по елементно. І виглядає у коді це так:
foreach($coord as $k => $xy){// по елементно передаємо дані з масиву 
    $c2=$xyf($xy);$c1=$xyf($coord[$t]);$t=$k;//
    imageline($im,$c1['x'],$c1['y'],$c2['x'],$c2['y'],$black);
}  
$ coord - an array of elements that is passed to the foreach construct. The 'as' operator specifies the variable to which the value from the array will be passed if '$a => $b ' $a key, $ b value . if after 'as' one variable, it is assigned the current value of the element in the array.
The loop ends when all elements of the array pass.;
$coord - масив елементів який передається у конструкцію foreach. Оператор 'as' вказує на змінну якій буде передано значення з масиву , якщо '$a=>$b' $a ключ $b значення. якщо після 'as' одна змінна , їй присвоюється значення поточно елемента у масиві.
Цикл завершиться коли пройде усі елементи масиву.

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


temp1.png



Sort:  

Отлично получилось! Мне понравилось, но язык ещё тот набор иероглифов для меня.

Loading...

You got a 9.09% upvote from @shares courtesy of @mandarin2016! This is a service sponsored by @Yehey. Please consider voting for @Yehey as one of your Witness. Use this short URL link https://on.king.net/witness simply click and vote, this will redirect to steem connect for secure connection.

If you need an extra upvote, join us at https://SteemChat.com discord server. Or simply hang out and meet new friends with common interest.

WANTED SP DELEGATION. Let your Steem Powers (SP) works for you. Delegate SP to receive daily payouts. Choose the amount of your delegation. Start small or go big, it's all up to you.
10 SP
50 SP
100 SP
200 SP
500 SP
1000 SP
2000 SP
3000 SP
5000 SP
10000 SP
20000 SP
50000 SP
100000 SP
500000 SP
1000000 SP

Have a fun day.
@Shares - Keep Steeming for a better future.

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.19
TRX 0.15
JST 0.029
BTC 63470.48
ETH 2544.22
USDT 1.00
SBD 2.72