Jump to content
  • entries
    128
  • comments
    16
  • views
    43,795

Tip-n-Trick: Gotcha’s When Assigning and Using Variables


eXcelisys

5,806 views

 


Gotcha’s When Assigning and Using Variables


 


by Andy Frazier


 
“Learn from the master, you must!”


 


"alt="Programming Languages" width="300" height="192" />


 


 


 


 


 


 


 


If you do any development outside of FileMaker Pro (and trust me, if only to learn different ways of approaching problems, you really should), you know that there’s a “gotcha” when assigning and using variables.


In several languages, you can assign a variable like this:


$myvariable = ‘my value';


Simple enough. But then when you want to test the variable, you can’t use “=” again, because that would just reassign it. So if, for example, you did this:


if ($myvariable = ‘something else’) {
everyoneDies();
}


…everyone would die…because you didn’t really test it – you just reassigned it. If the variable name comes first, “=” is an assignment operator. So in the example above, since you’re assigning a new value to $myvariable, and that will always work, the test will always return true. The test for equality, which is what you want here, is “==” – two equals signs (if you wanted to test for both type and content, you’d use three ["==="], but that’s only for really picky languages). So what that should have said was…


f ($myvariable == ‘something else’) {
everyoneDies();
}


…so everyone would live, because your original value of “my value” remains intact, and is not equal to “something else” – we’re all safe!


Unfortunately, every developer (yes, EVERY developer) has forgotten that second equals sign at least once. The fortunate among us have discovered that before the client demo. Those not so fortunate…well…please remember to give to charity.


There is, however, some Jedi magic we can use here. If we use “Yoda syntax,” we can avoid this mistake altogether. Consider this:


If (‘something else’ = $myvariable) {
everyoneDies();
}


In this case, we put the value first. Most language interpreters are smart enough to see that. So they will treat the single equals sign as an equality operator and not an assignment operator, and the test will be evaluated as intended. Everyone lives!!


May the force be with you.


 


About Andy Frazier: Andy, a http://www.excelisys.com/our-team-custom-database-consultants.php"target="_blank">Lead FileMaker Pro and Web Developer with Excelisys for 2 years, wrote his first program in BASIC on a Commodore 64 back in the early 80’s. So, having successfully conquered computers for all time, he took a break from them for about 20 years, playing guitar and writing music (his degree from Berklee is in Film Scoring; he’s written scores for video games and had songs in TV shows like The Sopranos & Charmed). When it turned out there just might be something more to this whole computer thing, he discovered FileMaker Pro, and realized not only that it was easy, it was fun, and it could make a difference by making many people’s jobs much easier. Now certified for FileMaker versions 8 through 13, along with Javascript, PHP, and jQuery, his goal is to create new solutions to improve someone’s life, somehow, some way, every day. Andy was the original developer for a very successful suite of solutions (which can’t be disclosed or he would have to kill himself) in the retail and service industries, he is also the author of several PHP-focused articles, and “the fixer” for countless issues ranging from overhauling failing businesses to solving sticky script situations. Andy has one philosophy with regard to thinking outside the box: there is no box. He feels the best part of developing with a tool like FileMaker Pro is using all the complimentary related technologies (PHP, XML, ODBC, CSS, HTML, Javascript) to do what each is best at and combining them into a whole complete-finished and polished solution which is more than the sum of its parts.


About Excelisys, Inc.: Founded in 2001, Excelisys (www.excelisys.com) is a dedicated organization specializing in the designing, developing, customizing, supporting, consulting, migrating, upgrading, tweaking, fixing, and integrating of FileMaker Pro solutions, FileMaker Go solutions, MySQL, PostgreSQL, QuickBooks-FileMaker Pro Integration, Excel and MS Access FileMaker Pro conversions/migrations, iPhone and iPad business solutions, and other various database technologies and frameworks that automate your organization’s data solution needs for use on the web, mobile, and desktop platforms. Contact Excelisys today for a free estimate and consultation about your business software automation needs @ 866-592-9235.


http://www.filemaker.com"href="http://www.filemaker.com/">* FileMaker and FileMaker Pro are registered trademarks and owned by FileMaker, Inc. in the US and other countries.



http://www.excelisys.com/blog/2014/11/13/php-tip-n-trick-gotchas-assigning-using-variables/"class='bbc_url' rel='nofollow external'>Source

0 Comments


Recommended Comments

There are no comments to display.

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.