php - Recursive function returning null but on var dump returns a value -
i creating function homecoming slug based on string entered, if slug exists on database append number -$num, using codeigniter,
when die variable returns proper slug on _generate_slug returns right value, when die on index function returns blank;
here's function
controllers/test.php
public function index() { echo $this->_generate_slug('test'); } protected function _generate_slug($string,$cntr = 0) { if($cntr == 0){ $slug = create_slug($string); }else{ $slug = create_slug($string).'-'.$cntr; } if($this->test_model->slug_exist($slug)){ $cntr++; $this->_generate_slug($string,$cntr); }else{ homecoming $slug; } } helpers/test.php
function create_slug($string) { $slug = strtolower( $string ); $slug = str_replace('&','and',$slug); $slug = preg_replace('/[%\'"``]/', '', $slug); $slug = preg_replace('/[^a-za-z0-9-]/','-',$slug); $slug = preg_replace("/[-]+/", "-", $slug); $slug = trim($slug, '-'); homecoming $slug; }
you getting null because don't homecoming value in recursion. should be:
return $this->_generate_slug($string,$cntr); but it's mentioned in comments, it's weird using recursion here.
and also, i'm not familiar code style in codeigniter, seems bad practice name protected methods starting underscore.
php
No comments:
Post a Comment