note 121326 deleted from control-structures.goto by salathe

From: Date: Mon, 21 Sep 2020 14:43:00 +0000
Subject: note 121326 deleted from control-structures.goto by salathe
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-215286@lists.php.net to get a copy of this message
Note Submitter: at YearOfCodes (retrobytespr at mail dot com) ---- Here is an example of re-using labels in separate methods - in this example, the end: label is used in each, with the goto condition behaving like an if/else condition: class DateController { public $day, $month, $year; public function __construct(){ $this->day = $this->setDays(); $this->month = $this->setMonths(); $this->year = $this->setYears(1901, (int)date('Y'), 'asc'); } /** * @param int * @return array */ protected function setDays(int $default = 0){ $days = array(); for($i = 1; $i <= 31; $i++){ $day = "{$i}"; if($i<10){ $day = "0{$i}"; } $days[$day] = $day; } if($default == 0){ goto end; } $days['default'] = $default; end: return $days; } /** * @param string, string, string * @return array */ protected function setMonths(string $type = "full", string $keyType = "numeric", string $default = ''){ $keys = array( 'numeric' => array( "01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12" ), 'full' => array( "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" ), 'short' => array( "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" ) ); $months = array(); $index = 0; foreach($keys[$keyType] as $primaryKey){ $months[$primaryKey] = $keys[$type][$index]; $index++; } if($default = ''){ goto end; } $months['default'] = $default; end: return $months; } /** * @param int, int, string * @return array */ protected function setYears(int $start = 1977, int $end = 2017, $order = "asc"){ $years = array(); if($order == "asc"){ for($i = $start; $i <= $end; $i++){ $years["{$i}"] = $i; } goto end; } for($i = $end; $i >= $start; $i--){ $years["{$i}"] = $i; } end: return $years; } }

« previous php.notes (#215286) next »