php - Function returns lots of notices -


First of all, my skill level is the best, a hobbit - I'm new at all, if not all of you.

I am trying to create a function that is a & lt; Br / & gt; will return if no parameter / value is returned, many & lt; Br / & gt; tag is equal to $ num

I am trying to create this function:

a) How to create a function

b) I & lt; Br / & gt;

C) I hate to type, this is the idea of ​​the function that came with interest to me.

So, ideally, in my code, if type getBrT () , this is a single return the tag, if I type << code> getBrT (1) , it also has a single & gt; return the tag, but if type getBrT (5) this five & lt; Br / & gt; return the tag.

This is not working because I hope that when I type getBrT () . Do I always have a price to flatten? Am I trying to not be possible within PHP's obligations?

Here is the function I have created:

  function getBrT2 ($ num) {// If the number is equal to 'nothing', 1 break - repeatedly Easy to call / Type the code ($ num = '') {echo '& lt; Br / '; } Else {// If the number is equal to 'some', breaks equal value $ i = 0; // Initial Counter ($ i & lt; $ num) {echo '& lt; Br / & gt; '; // sal = i ++;}}}    

to ensure that This function does not give any value to the function, a default value is set.

  function getBrT2 ($ num = 1) {// default value 1 $ num = (int) $ num; // if the number is negative ($ num & lt; 1) {// if the number is negative 0 or negative then it would be numerically numerator of 1 $ num = 1; } Return str_repeat ('& lt; br / & gt;', $ num); // many & lt; Br / & gt; As requested, echo getBrT2 (1); // prints "
" Echo getBrT2 (5); // print out "






" Echo getBrT2 (''); // prints "
" Echo getBrT2 ('hello'); // prints "
"

I made some improvements to your function.

  • In addition to the default value, I put the value passed to an integer, because we need to work with it. Any string value that does not begin with a number will be converted to 0 .
  • Then we check that is $ num is 0 or if a negative number, then we make it 1 .
  • Then we need as many as str_repeat () as many & lt; Br / & gt; tags
  • Then we return that string, so that it is echoed.

Comments

Popular posts from this blog

java - ImportError: No module named py4j.java_gateway -

python - Receiving "KeyError" after decoding json result from url -

.net - Creating a new Queue Manager and Queue in Websphere MQ (using C#) -