Anti-Moronic Posted July 3, 2009 Share Posted July 3, 2009 Love to know if anybody uses any shorthand they find extremely useful. ANYTHING that reduces typing but maintains functionality, even at the expense of readability, for some ..and one moronic question... I use this a lot: $string = clean(string); $string = strtoupper($string); etc. etc. strtoupper could be any number of functions I use. Is there a way to simply run this without $string = ? e.g.. $string = clean(string); strtoupper($string); Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/164652-php-shorthand/ Share on other sites More sharing options...
GingerRobot Posted July 3, 2009 Share Posted July 3, 2009 Well i suppose you could write your own function and pass the argument by reference: <?php function mystrtoupper(&$string){ $string = strtoupper($string); } $string = "mystring"; mystrtoupper($string); echo $string . "\n"; ?> But i don't see much point. Quote Link to comment https://forums.phpfreaks.com/topic/164652-php-shorthand/#findComment-868309 Share on other sites More sharing options...
Anti-Moronic Posted July 3, 2009 Author Share Posted July 3, 2009 You're right, there isn't much point in passing anything to a function. The strtoupper can be any number of native functions. It's just a simple example of the kind of shorthand I'm looking for (anything to cut corners) and also something I asked myself today. I am probably missing something fundamental about how php executes native functions. What is &$string for? does that make it global and override the previous variable? What shorthand do you use? Anything that, when you first started using, thought how did you ever code without it? Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/164652-php-shorthand/#findComment-868312 Share on other sites More sharing options...
.josh Posted July 3, 2009 Share Posted July 3, 2009 &$string does not make $string global. It means to pass a variable be reference. For instance, if you have this: function something ($string) { $string = "bar"; } $string = "foo"; echo $string; // output: foo something($string); echo $string; // output : foo You first assign "foo" to $string. You echo it out and it is "foo". You then call your function, passing $string to it. Inside the function, you change the value of $string. You then echo $string again, after the function call. It is still "foo" because the $string inside your function is not the same variable as the $string outside of your function. When you pass something to a function, you are passing a copy of the value "foo" to it. &$string means to pass by reference. What that means, is instead of telling the function to work with its own copy of the value, you want to work with the reference to the original value. So for instance: function something (&$string) { $string = "bar"; } $string = "foo"; echo $string; // output: foo something($string); echo $string; // output : bar In the function argument, since you are specifying it as being passed by reference, any changes to $string inside the function will be to the original value. So when you echo $string out after the function call, it will be "bar". This is not the same as making it global. It's just that in in this situation, it mimics a global variable/basically acts the same way. However, it is important to understand that it is NOT the same as making a variable global. First off, passing a variable by reference does not have to come from the global scope. You can call a function inside another function, passing a variable by reference, and that won't be tied to the global scope. For example: function something (&$blah) { echo $string; // output: <nothing. this variable doesn't exist> $blah = "bar"; } $string = "foo"; echo $string; // output: foo something($string); echo $string; // output : bar As you can see, I passed $string to the function by reference, but I called it a different name in the argument. So when I try to echo $string inside the function, nothing is echoed. So passing by reference does not make something global. It's just that when you are passing by reference from the global scope and you make the argument var name the same as the global scope var name, it just makes it look like it is global. But they still have their own separate namespace, pointing to the same value. 2nd, when you pass something by reference, php still creates a variable name with its own namespace. But it points that new variable to the same location in memory holding "foo" as the original variable. So for example, you could do this: function something (&$blah) { $blah = "bar"; } $string = "foo"; echo $string; // output: foo something($string); echo $string; // output : bar So you see, I pass $string to the function, and in the function argument, I specify it to be passed by reference, but I did not name it $string. I then set $blah to "bar" and as you can see from the 2nd output, $string gets changed to "bar". So a more graphic representation might look like this: Regular variable passing: scope variable value memory address global $string foo 1234 function $string bar 1235 Passing by reference: scope variable value memory address global $string foo 1234 function $string bar 1234 or scope variable value memory address global $string foo 1234 function $blah bar 1234 Or another way of saying it (the more classic way) is to think of a variable as a box with a label on it. The box holds the info and the label is the variable name. So when you pass a variable to a function, you are creating a new box with a new label, that is a copy of the first box. It may or may not have the same label. Contents of box two equal box one, but they are not the same entity. But when you pass by reference, it's like you are adding a 2nd label to the box, instead of creating 2 boxes. Quote Link to comment https://forums.phpfreaks.com/topic/164652-php-shorthand/#findComment-868333 Share on other sites More sharing options...
Mark Baker Posted July 3, 2009 Share Posted July 3, 2009 Chain the functions together? $string = strtoupper(clean(string)); Quote Link to comment https://forums.phpfreaks.com/topic/164652-php-shorthand/#findComment-868336 Share on other sites More sharing options...
.josh Posted July 3, 2009 Share Posted July 3, 2009 As far as "shorthand" tricks... if you have to ask then you probably shouldn't be using them. I swear, no offense intended. But the reason shorthand tricks have evolved is from people repeatedly doing things and they understand the concept so well that they can take that next step and make a shorthand version of it. But if you skip that process, you are more than likely going to wind up confusing yourself and making some piece of code or whole system that is all ass backwards and broken because you didn't understand what that "shorthand" really meant. For example, I can't tell you how many times on the forums I've seen people try to use the ternary operator because it looks cool, but end up F'ing it up because they don't really understand it, what it and its limitations vs. its fully typed out counterpart. Quote Link to comment https://forums.phpfreaks.com/topic/164652-php-shorthand/#findComment-868339 Share on other sites More sharing options...
Anti-Moronic Posted July 3, 2009 Author Share Posted July 3, 2009 Hey, thanks for that very well put post. Will be reviewing that later. Don't worry, no offense taken, afterall, you can't possibly assume my knowledge simply because I haven't used &$var before. In the past, I learned and developed through necessity and now I'm taking a more proactive approach. If you do actually have some useful shorthand you find yourself using a lot, even touching on it is enough for me to find more info. I can't find a good, single source for this kind of stuff. If I don't understand your shorthand, no problem, I can always look up the full typed version and *learn*. That's why I'm here. I don't want your assumption of my knowledge to deter you from offering advice, surely that is directly contradicting to the nature of this forum..? Maybe a better question would be: -- what common 'long winded' code do you see often which gets on your nerves, and what is the shorthand equiv? Thanks for your input, truly appreciate it! Quote Link to comment https://forums.phpfreaks.com/topic/164652-php-shorthand/#findComment-868351 Share on other sites More sharing options...
Adam Posted July 3, 2009 Share Posted July 3, 2009 I don't understand the great need for shorthand workarounds? The odd second or two you may save yourself here n there in future won't count for anything against the many hours you spend looking them up and learning them. Just pick them up naturally whilst learning more useful things... I suppose a good analogy is moving to a new town and trying to find someplace by taking every short cut you find, without going down the main roads a few times first? Ha.. second thoughts perhaps a little corny! Quote Link to comment https://forums.phpfreaks.com/topic/164652-php-shorthand/#findComment-868389 Share on other sites More sharing options...
Anti-Moronic Posted July 3, 2009 Author Share Posted July 3, 2009 perhaps lol I like the analogies, even 8ft trolls that never shut up. You make a good point, is it really worth my time to go out of my way to discover some shorthand. Maybe not for the tiny instances I illustrated above, but for conditionals and more naturally long-winded approaches, I think there is a point. Like with ternary conditionals (which someone so gracefully used in another post a second ago). They significantly reduce the amount of code required. It's not necessarily about time in my case, I like my code to be extremely compact. Ternaries are definitely next on the list! So, a more refined question: Are there any other shorthand techniques, similar to ternary operators, which significantly reduce code? Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/164652-php-shorthand/#findComment-868406 Share on other sites More sharing options...
.josh Posted July 3, 2009 Share Posted July 3, 2009 As mentioned before, there is the ternary operator that is sort of a shorthand version of if..else. There's also the "$this->xxxx" shorthand for OOP. But I can't really think of anything else. Programming languages don't generally have long and short versions of the same thing. If the short version does the exact same thing as the long, there's no reason for the long version to be there. So there really isn't any "shortand" (other than the ones I mentioned, afaik) things, in the sense that you are talking about. But if you look at in a more general sense of "How can I make this chunk of code smaller, more efficient, etc.." there are probably plenty of ways to do that, but it would be specific to the situation. There are plenty of ways to capitalize on what lots of different built-in functions do, what they produce, etc.. I guess overall what I'm trying to say is the built-in functions are the shortcuts, so the more you learn about them, use them, figure out their nuances, etc.. the more you can look at a long piece of code and be like "hey, since abc function does this and xyz function does this other thing, I can combine them in this way to effectively do the same thing as this long chunk of code!" So as for a physical list, I guess the best thing to offer up is to tell you to go to the manual and go down the list of functions. Learn them. Memorize them. Read what they do, what the arguments do, what data types they can take. How they behave depending on what you push to it. Read the user notes for each of them. They are full of "hey, here's a clever thing to do with this!" or "Hey, here's an unexpected behavior from this..." type of posts. And from there, just practice. Go find random pieces of code. Look at it and think about all that info from php.net you just crammed in your head. Figure out what it is that chunk of code is doing and think about if there is an existing function that already does it, or if combining existing functions produce the same result, etc... p.s.- response to your last post since you posted when I was typing this: It is important to understand that a ternary operator is NOT the exact same as if...else. It has certain limitations to it. Quote Link to comment https://forums.phpfreaks.com/topic/164652-php-shorthand/#findComment-868409 Share on other sites More sharing options...
Anti-Moronic Posted July 3, 2009 Author Share Posted July 3, 2009 Thank you very much for that Crayon. I use $this when doing OOP... I didn't in the start until I asked similar questions to this. Same with ternaries, which I only discovered under the same circumstances. I think that's the main reason why I've prompted such responses again. ..and you've pointed out something so obvious which I kind of overlooked: "Programming languages don't generally have long and short versions of the same thing. If the short version does the exact same thing as the long, there's no reason for the long version to be there" I started with simple languages like html and css, so php is my first venture into a real programming language, so to speak. Another great point as well about the built in functions acting as shortcuts to potentially larger blocks of code. I think that is actually what I'm looking for. Like a list of common code structures which can be reduced by the use of native functions. Can't tell you how long I used things like: $n=1; foreach($array as $arr){ $n++; } $arraynum = $n; instead of: $arraynum = count($array); Think I'll start scanning these functions to truly grasp the power at my finger tips, then learn to command it! Understood about the ternary operators. From what I do know, they are extremely limited compared to normal if...else statements, but they certainly have their place. Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/164652-php-shorthand/#findComment-868418 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.