php - Function returns lots of notices -
php - Function returns lots of notices -
first off, skill level @ best, hobbit - new compared if not of you.
i attempting craft function homecoming single <br />
if no parameter/value given, else homecoming many <br />
tags value $num
equal to.
i'm trying create function to:
a) larn how create functions
b) hate typing <br />
c) it's thought function came interested me.
so, ideally, in code, if type getbrt()
, homecoming 1 single <br />
tag, if type getbrt(1)
, homecoming 1 single <br />
tag, if type getbrt(5)
, homecoming 5 <br />
tags.
its not working hoped when type getbrt()
. have slug value in? trying not possible within constraints of php?
here function i've built it:
function getbrt2($num){ //if num equals 'nothing', 1 break - easier call/type in code repeatedly if ($num = ''){ echo '<br /'; }else{ //if num equals 'something', breaks equal value $i = 0; // initialize counter while ($i < $num) { echo '<br />'; // increment counter $i++;} } }
to create sure works when no value passed function set default value.
function getbrt2($num = 1) { // default value 1 $num = (int) $num; // cast num integer if ($num < 1) { // if num 0 or negative create 1 $num = 1; } homecoming str_repeat('<br/>', $num); // echo out many <br/> requested } echo getbrt2(1); // prints out "<br/>" echo getbrt2(5); // prints out "<br/><br/><br/><br/><br/>" echo getbrt2(); // prints out "<br/>" echo getbrt2(''); // prints out "<br/>" echo getbrt2('hello'); // prints out "<br/>"
i made few improvements function.
besides default value cast value passed integer since that's need work with. string values not begin number converted0
. then check if $num
0
or negative number. if so, create 1
. then utilize str_repeat()
create out many <br/>
tags desired. then homecoming string can echo'd out. php string function parameters var
Comments
Post a Comment