22: How to Create Your Own Function in PHP | PHP Tutorial | Learn PHP Programming | PHP Lesson

แชร์
ฝัง
  • เผยแพร่เมื่อ 3 ม.ค. 2025

ความคิดเห็น • 70

  • @waitajoseph1
    @waitajoseph1 5 ปีที่แล้ว +9

    I think your tutorials are the best. Finally, I know how to use PHP practically. I like the way you keep things simple, no complicating matters. Indeed, simplicity is the ultimate sophistication.

  • @kiiturii
    @kiiturii 4 ปีที่แล้ว +14

    these are so good. All the other videos I've seen feel like they're not teaching you at all they just show you that *they* can do it...

  • @zedpiecce6116
    @zedpiecce6116 6 ปีที่แล้ว +2

    I have watch many php program toturial..... But you are the only one why simplify for the lane man.....thank you very much

  • @AdrieShahla
    @AdrieShahla 6 ปีที่แล้ว +10

    Good tutorials man :) I just have to clarify that the function is not looking for $x. You can call a variable inside the parameters any way you want(like $y). What matters is what you pass trough to that function when you are calling it in newCalc(&x). We are passing value 100 that is contained inside $x and function takes it and saves it into $y.

    • @therappah
      @therappah 6 ปีที่แล้ว

      good call, was wondering the same thing

    • @Redmile2006
      @Redmile2006 6 ปีที่แล้ว +1

      Or you can do like this if you don't want to pass it:
      $x = 100;
      function newCalc() {
      global $x;
      $newnumber = $x * 0.75;
      echo "Here is 75% of what you wrote: $newnumber";
      }
      newCalc();
      But you should rarely do this with functions, you should pass arguments. However, I have found cases where it is useful in Python when making a package, and I'm sure there are cases in PHP where it would be useful.

    • @jeffreydelossantos8667
      @jeffreydelossantos8667 6 ปีที่แล้ว

      that's true. The function parameters is local to the function itself, you can name the parameters anything you want because it is not related to the variables you named globally

  • @hehhehdummy
    @hehhehdummy 7 ปีที่แล้ว +45

    *Expand this comment if you're a beginner who is confused.
    *Note for non-beginner programmers: from within the local scope of a function, a global variable can only be accessed if you first write the keyword 'global' before the variable name. There's also a PHP defined array named $GLOBALS that uses the name of the globally defined variable as the accessor(e.g., $GLOBALS['varName'])
    I love all of your videos so far. I think that this video is treading dangerous territory. I'm sure you go over this later on(confirmed that he does), but I believe this video should have directly addressed what variable scope is. The explanation of functions was uncharacteristically unorganized from what I've seen of your tutorials. I believe you should have explained functions that don't take arguments. Then explained functions that do take arguments. Then explained that functions can return values as well.
    I know when teaching about programming, sometimes it's best to just say "we'll explain why later". In this case, I think the order in which things are discussed could easily be confusing.
    If you are confused I recommend looking at this page: www.w3schools.com/php/php_variables.asp
    Specifically the section about scope that talks about local and global scope and how to access a global variable from within a function.
    Also look at this page: www.tutorialspoint.com/php/php_functions.htm
    Specifically the sections: Creating PHP Function, PHP Functions with Parameters, Passing Arguments by Reference, and PHP Functions returning value.
    It may seem like it's a lot of material, but It's all straightforward and are consistent properties of most programming languages, which is why even though I'm learning php like you, I understand those concepts. I don't think it will take much time at all to absorb what you need to know.

    • @mek059
      @mek059 7 ปีที่แล้ว +1

      the parameters inside a function are instead of simply writing $param for example inside the function?

    • @Sephsplaces
      @Sephsplaces 5 ปีที่แล้ว +1

      super glad i read this. coming from java, i was a bit confused why you couldn't just use a non-local variable, especially as I had already made some code which I had instantiated a class and used $this to assign a variable outside the function.
      edit. infact the video is really weird about explaining local variables

    • @ShahinShateri
      @ShahinShateri 4 ปีที่แล้ว

      I think it's kinda confusing the newbies. the function does NOT look for the variable from outside but we simply pass the value/parameters to the function and it doesn't matter what will the name of the variable that has been passed to the function be. you just have to pass the parameters in the same order.

    • @letsgocamping88
      @letsgocamping88 4 ปีที่แล้ว

      Glad to see this comment clearing it all up.

  • @samy_akrouh
    @samy_akrouh 2 ปีที่แล้ว +1

    I have a question in minute 6:37, is that a meowing language?

    • @Dani_Krossing
      @Dani_Krossing  2 ปีที่แล้ว

      That would be Misse, my wifes cat. 🙂

  • @vojka2973
    @vojka2973 8 ปีที่แล้ว +2

    I just wanna thank you for these awesome tutorials and also thank you for replying my question. I couln't be enjoying this more than I am right now :)

  • @huckleberrymack7865
    @huckleberrymack7865 3 ปีที่แล้ว

    Thank you for these videos, they are absolutely wonderful to compliment my college coursework!!

  • @purnarai76
    @purnarai76 2 ปีที่แล้ว

    Thanks sir for such a great teaching. I hope I can study php completely through this chanel.

  • @aderickzaki2122
    @aderickzaki2122 2 ปีที่แล้ว

    thank you dan👋 am enjoying your tutorials

  • @riaznapa
    @riaznapa 5 ปีที่แล้ว

    Thanks for the video, for a beginner like me it is really easy to understand.

  • @FskpZ
    @FskpZ 6 ปีที่แล้ว +1

    So to sum up:
    Parameters in the functions is like bridges between external code (outside of the function) and inside code (between { }).
    Everything I wont to pass from function our in function need to go through parameters?

  • @galynag-ei5vv
    @galynag-ei5vv 2 ปีที่แล้ว

    i'm very sorry, i thought i subscribed to you channel long time. you are really a pro and a teacher. i love your explanations very much. even if you sell your tutorials to me for $1000000000000 i will buy

  • @pratikthorat3480
    @pratikthorat3480 4 ปีที่แล้ว +1

    just a doubt: why not use the break inside the function itself. i mean the line where you've written
    echo "This is 75% of what you've written".$newnr. "". That way you can actually avoid writting echo "" all the time

  • @AndyInLearning
    @AndyInLearning 8 ปีที่แล้ว

    Hi, Daniel. Your PHP tutorial is one of the best I have watched. Thank you. Just curious, where you from?

    • @Mike-ip1el
      @Mike-ip1el 7 ปีที่แล้ว

      He's from denmark

  • @pritalmav7207
    @pritalmav7207 4 ปีที่แล้ว

    Best Tutorial thanks a alot

  • @voiceluckan
    @voiceluckan ปีที่แล้ว

    Hi Dani, great content. One question, how do I create an open variable, where the user is the one that inputs the value so that the answer will vary accordingly??

  • @figmauser1996
    @figmauser1996 4 ปีที่แล้ว

    Thank you brother.

  • @nigelpallatt
    @nigelpallatt 7 ปีที่แล้ว +3

    echo "75% of $x is $newnr"; Use dubel quotes then you dont need the , or .

  • @saifurpro
    @saifurpro 5 ปีที่แล้ว

    Which "theme" and "Color Scheme" did you use in your Sublime text? Please tell me. I like the UI.

  • @mixalisiakovou2233
    @mixalisiakovou2233 6 ปีที่แล้ว

    Hello! what theme and color scheme does you use?

  • @eosapril
    @eosapril 9 ปีที่แล้ว +9

    When you finish the beginer tutorials try sell the next tutorials advanced, i will definitely buy, your tutorials are the best.

    • @ScreenPrintR
      @ScreenPrintR 8 ปีที่แล้ว +20

      Or do the advance for free, and you'll earn your money from youtube.

    • @__________2756
      @__________2756 6 ปีที่แล้ว +4

      What ar u ? Donald trump?

    • @jumpkari
      @jumpkari 6 ปีที่แล้ว +2

      I'd pay for advanced tutorials from you as well. Great teacher.
      Eos April he has a Patreon account, donate.

  • @jasurjonmamatkulov5057
    @jasurjonmamatkulov5057 4 ปีที่แล้ว

    Hi Daniel, Thank You for videos. Could You tell me your Sublime Theme name ? or Was it only
    an old version?

  • @rhyswilliams4304
    @rhyswilliams4304 6 ปีที่แล้ว

    Also do modules function the same way?

  • @ahammedimtiaze
    @ahammedimtiaze 6 ปีที่แล้ว

    what ide u have used for this course?

    • @Dani_Krossing
      @Dani_Krossing  6 ปีที่แล้ว

      I didn't use an IDE, I used a Text Editor named Sublime Text :) However today I have switched over to Atom.

    • @ahammedimtiaze
      @ahammedimtiaze 6 ปีที่แล้ว

      @@Dani_Krossing Thank you

  • @greenmansession
    @greenmansession 5 ปีที่แล้ว

    I dont have the funds to donate on patreon, but I really appreciate the free learning you offer- you're a fantastic teacher so i white-listed your TH-cam account on adblock just to give you that little ad revenue. Thanks Daniel.

  • @nogafouz2174
    @nogafouz2174 2 ปีที่แล้ว

    merci beaucoup

  • @MdMorshedAlamBadhan
    @MdMorshedAlamBadhan 8 ปีที่แล้ว

    Hi, thank you for the tutorial.. but have a question..
    you created a function with variable $x.. after that you changed the variable to $a.. but you did not change the insider of that function.. but it worked...! how? need a little bit more please.. :-)
    I'm new in php, you can tell.

    • @Dani_Krossing
      @Dani_Krossing  8 ปีที่แล้ว +2

      The $x inside the function is just a placeholder :) it is just there to show where inside the function it should continue to use whatever you place in the function when we use it later on.

  • @rw7799
    @rw7799 7 ปีที่แล้ว

    good stuff...what I don't really get is that lessons seem to show how code reacts to values put in by the programmer.... how do values (that always change) by random users on a website for example get processed?

    • @kefgeer5403
      @kefgeer5403 7 ปีที่แล้ว

      User a form with HTML

  • @rhyswilliams4304
    @rhyswilliams4304 6 ปีที่แล้ว

    one question on Functions, how does the function process the expressions within the ()brackets by default? by reference? or by value? the way you expressed it in this video it seems that it is by reference, but one statement you made makes it sound like it process by value as well. however, you did not define it as by ref or by val.

    • @kistra2244
      @kistra2244 6 ปีที่แล้ว

      Hi Rhys.
      I think that when you define the function, it proccess the expression within the bracket as a reference and when you call it as a value.
      I'm not 100% sure about that, but I think that's how it works.

  • @freesoul3336
    @freesoul3336 7 ปีที่แล้ว

    best tutorial

  • @eosapril
    @eosapril 9 ปีที่แล้ว

    Do you have Class || OOP? thx ;)

    • @Dani_Krossing
      @Dani_Krossing  9 ปีที่แล้ว

      +Eos April Not yet but will get into it in the future

    • @eosapril
      @eosapril 9 ปีที่แล้ว

      +mmtuts Ok Boss ;) Until then, my eyes are on you || i loop your tutorials until i became a master on PHP ;) ja ja ja, sweet dreams...

  • @sdy.deathcall9608
    @sdy.deathcall9608 3 ปีที่แล้ว

    In your first line of code you put a variable $x in the parentheses. $x is present outside and so is $a so why not put $a in there instead of $x? And it would be easier to uN if the parentheses remained empty

  • @christopherdavis4136
    @christopherdavis4136 5 ปีที่แล้ว

    I don't quite understand about which variables go in the parentheses and what is meant "to call" a variable exactly

    • @hdphoenix29
      @hdphoenix29 5 ปีที่แล้ว

      if you put for example a $x in parantheses means that your function need to get a value from user
      For example :
      function myFunc($x) {
      if ($x % 2 == 0){
      echo 'the number is even';
      }
      else {
      echo 'the number is odd';
      }
      }
      //now you give a value to your function
      myFunc(20);
      //here you can put any number and it goes instead of x in function
      //now if you check your browser it shoud tell that the number is even
      //if you put 5 it tells that the number is odd
      i hope you got that

  • @sdy.deathcall9608
    @sdy.deathcall9608 3 ปีที่แล้ว

    Someone please explain: the function newcalc deals with $x and in said code $newnr is multiplying $x by 0.75 for its value. So. How can calling newCalc($a) actives the function? When $newnr is using the value of $x???

    • @nuysance8797
      @nuysance8797 3 ปีที่แล้ว +1

      yeah well youd have to rename the $x or add another function $a to actually trigger the function

  • @abdirazakelmi6993
    @abdirazakelmi6993 5 ปีที่แล้ว

    Cant you just do newCalc($x,$a), instead of having returning it twice?

  • @thales-maciel
    @thales-maciel 5 ปีที่แล้ว

    Parse error: syntax error, unexpected 'newcalc' (T_STRING)

  • @alexzil2142
    @alexzil2142 4 ปีที่แล้ว

    Isn't there any update??? this is like 5yrs ago

  • @jrhager84
    @jrhager84 3 ปีที่แล้ว

    You're conflating variables and parameters. You could just as easily omit the global variable $x and just pass the parameter as a number in the function call that will be used by reference.
    I'd highly avoid this type of name collision as it's really hard to follow the code flow.
    The function isn't 'looking' for a variable anywhere other than as the parameter in the function's invocation. function(2) for instance. It just assigns 2 to $x and then runs the function.
    You're passing the value of $x by reference to the invocation, which is then (confusingly) assigning it to a local variable of the same name.
    You can test it by changing the variable name to $n in the global scope and the function parameter and it will still work as intended.
    👍

    • @Dani_Krossing
      @Dani_Krossing  3 ปีที่แล้ว +1

      Yes I'm quite aware hehe 😊 If I'm not mistaken, I explain that at some point near the end. (or I'm remembering wrong and I do that in my JS course)

    • @jrhager84
      @jrhager84 3 ปีที่แล้ว

      @@Dani_Krossing a minor quibble, of course.
      Good content. You have a knack for it. 😎👍

  • @cjvaans4484
    @cjvaans4484 5 ปีที่แล้ว

    You're explanations are a bit too fast, but still helpful.

  • @nabeeltech7885
    @nabeeltech7885 6 ปีที่แล้ว

    they are OOPS concepts.

  • @tonysteens4090
    @tonysteens4090 3 ปีที่แล้ว

    It's confusing when you use $x both as variable and as parameter... Personally I use different names to prevent my brain to cook

  • @junharvs6962
    @junharvs6962 3 ปีที่แล้ว

    I tried this!

  • @thatsoofingreat8295
    @thatsoofingreat8295 8 ปีที่แล้ว +2

    you can also do this:

  • @dantewins
    @dantewins 4 ปีที่แล้ว

    hi

  • @Wreighn
    @Wreighn 8 ปีที่แล้ว

    You didn't mention the use of return inside a function, and the possibility of inputing different parameters separated by a comma.
    Thats something pretty basic.