Added: 3 years ago
From: phpacademy
Views: 21,205
Sort by time | Sort by thread (beta)

Link to this comment:

Share to:

All Comments (38)

Sign In or Sign Up now to post a comment!
  • if it goes to default, it'll be smth like echo echo "Unknown opreator.";

    You could have given the variable $total = "Unknown operator"; and just return it at the end of the function. Nice video, btw! :)

  • Comment removed

  • @andrewvpover you need the return statement at each of the case statements because once the processor hits the break; it exits the switch statement, so it doesn't process any more code within the switch.

  • Hope you know you can set the return once just return the total after the switch statement and have a if statement that only if total isn't in error or blank or null then return $total. that be more efficient way of doing it.

  • it's important to have in mind

    $num2 must be different than 0 in case of division.

  • it's important to have in mind

    $num2 must be different than 0 in case "/" :

  • Beautiful

  • thank you Alex, i have been watching your video for a while how. and every time that i watch a video i actually learn something. i really enjoyed that jquery refresh tutorial.

  • no need to break after a return...

  • hello alex, great tut... I am working through dreamweaver....I discovered that the RETURN function do not work instead it was ECHO function...why is that?

  • One question, why do you do "$" before num ?

  • @VisualIlusion watch his previous videos...

  • @VisualIlusion The sign $ is required for PHP variables.

  • @Medo3337 Thanks and, where'd you learn all this?

  • @VisualIlusion There are alot of resources, you can check PHP official website.

  • @VisualIlusion w3schools. google it

  • @jaradgrant1 Thanks man!

  • @VisualIlusion most php programmers are home tought, just google php tutorials

  • @madmatthew1 Thanks!

  • Larry Ullman says that you can RETURN $variable too, but what is the $variable?

  • In this tut Alex contains with functions and how to declare a function which return a numerical/string value .

  • This isn't secure, but it does the same job :-)

    function calc($num1,$num2,$op){ return eval("return $num1$op$num2;");

    }

  • It's alright but why do you return $total 4 times instead of using it once in the end of the function?

  • @mega3a4ot if you do it like this you need to declare default as $total = "unknown operator.", or you will get an unknown variable error because there is no $total in default

  • @mega3a4ot because if you return it at the end of the the function if you have an unknown operator then you will have an error.

  • @mikekevinvideos

    Not if you check that the value of the $total variable isset.. You should be checking the value before returning it, anyway.

  • @mega3a4ot That what I wanted to say when I saw it.

  • What is the difference in using the "switch" and "case" statements and using if else statements?

  • If -n-else statements are mostly for control statements, switch statements are using mutiple decision statements for a user can select from.  Case is specifing the condition you are chosing. You can say case or switch statement, but you have to speific switch() { case "": } .

  • You've lost me. I'm not sure I understand the advantage of using one over the other.

  • Ideally* -not all, feels like it matter which one should be use-, programmer should use switch-case if he/she have more questions for the end-user to make;such as, menu-base feature, and If/else statements need to use when simple decisions or data control for your program.

  • is not possible to define right $total in the function, and then in each "case" of switch, give the value of the choosen operationen.

    At the end we just "return $total" at the end of the function.

    This can spare us define a new "$total" variable in each case.

  • Can RETURN be substituted by ECHO in functions? or does it have to be RETURN all the time in functions.

  • Yes, you can echo it out directly from inside a function.However it's better to leave it as a RETURN and then echo it out as you require. Reason being you might want to use the function to do something else later on (i.e. string length) so in this case having it ECHO out as standard won't work.

  • @olie04 NO you cannot use return to echo, if you like just to echo it out then you will need to do echo(1,5,"+"); in the function for each switch case.

  • If your like me and like to write a little code after each tut than a "challenge" you can do is to write a calc with all the same functions as above but with less code (you can do it with what we have learned so far!)

  • Oh cool, the return command is pretty neat! Now you can only return one value to the function right?

Loading...
Alert icon
0 / 00Unsaved Playlist Return to active list
    1. Your queue is empty. Add videos to your queue using this button:
      or sign in to load a different list.
    Loading...Loading...Saving...
    • Clear all videos from this list
    • Learn more