Welcome Guest!
Please login
If you do not have an account yet on The Web Squeeze forums, please Register! It’s FREE and there are many benefits:
- Receive Fast Advice
- Learn Programming Languages
- Get Professional Website Reviews
- Quick Troubleshooting Assistance
|
|
Passing Php Variables To Javascript
This is a discussion on Passing Php Variables To Javascript, within the PHP section. This forum and the thread "Passing Php Variables To Javascript" are both part of the Programming Your Website category.
![]() ![]() |
Jul 14 2008, 10:20 AM
Post
#1
|
|
|
Squeeze Machine ![]() Posts: 682 Joined: 15-February 08 From: UK |
I want to use some PHP variables in my javascript. How do I do this?
|
|
|
Jul 14 2008, 11:24 AM
Post
#2
|
|
|
Squeeze Machine ![]() Posts: 682 Joined: 15-February 08 From: UK |
Well, I seem to have worked it out:
CODE $phpVar = "This variable was declared in PHP"; echo"<script type=\"text/javascript\">var jsVar = \"$phpVar\"</script>"; ...but if anyone knows how to avoid declaring a global variable, let me know. That would be a nice coding-purity touch. |
|
|
Jul 14 2008, 02:28 PM
Post
#3
|
|
![]() Rapid Squeezer ![]() Posts: 199 Joined: 14-February 08 From: Willich, Germany |
Well, as long as the Javascript is inline, you can do what you did where ever you want, the variable don't have to be global...
CODE <?php $php_var=34; ?> <script type="text/javascript"> function bla() { var blubber=<?php echo($php_var); ?>; } </script> This may be obvious, but i can't think of anything smarter at the moment... or i misunderstood the question.... (most likely the latter). -------------------- www.c010depunkk.com ~ the hangout of a web developer
|
|
|
Jul 14 2008, 08:09 PM
Post
#4
|
|
![]() Squeeze Machine ![]() ![]() ![]() ![]() ![]() Posts: 733 Joined: 13-February 08 From: Borneo |
Yeah I remember learning this trick with Rakuli
-------------------- |
|
|
Jul 14 2008, 08:28 PM
Post
#5
|
|
![]() Squeeze Machine ![]() Posts: 766 Joined: 13-February 08 From: Catching the squeezed drips downunder. |
If you wanted, you could set up a factory function on your external js file
CODE function makeAnObjec(give, me, some args) { this.give = give; this.me = me; this.some = some; this.args = args; } And if you already are declaring some variables inside of your external .js file, it's just a matter of adding a property when you write it out with Pph CODE alreadydeclaredVariable.phpPassedProperties = new makeAnObject(<?php echo '"give", "me", "some", "args"; ?>); So now you are using an object that is already in existence by creating another object on page load -- no namespace issues arising apart from those that are already present. -------------------- |
|
|
Jul 14 2008, 08:39 PM
Post
#6
|
|
![]() Squeeze Machine ![]() ![]() ![]() ![]() ![]() Posts: 733 Joined: 13-February 08 From: Borneo |
Oh this is beyond my brain
-------------------- |
|
|
Jul 15 2008, 12:40 AM
Post
#7
|
|
![]() Rapid Squeezer ![]() Posts: 199 Joined: 14-February 08 From: Willich, Germany |
If you wanted, you could set up a factory function on your external js file See, there's the clever answer I was looking for -------------------- www.c010depunkk.com ~ the hangout of a web developer
|
|
|
Jul 15 2008, 02:41 AM
Post
#8
|
|
|
Squeeze Machine ![]() Posts: 682 Joined: 15-February 08 From: UK |
If you wanted, you could set up a factory function on your external js file CODE function makeAnObjec(give, me, some args) { this.give = give; this.me = me; this.some = some; this.args = args; } Ooooh, this looks interesting. I might need some more guidance on this later, but first I should play with it and see what happens. QUOTE And if you already are declaring some variables inside of your external .js file, it's just a matter of adding a property when you write it out with Pph Except I'm not (I only declare local variables; when I need a global variable, which is rarely, I create a function instead). So how would I access this factory data without attaching it to a global variable? By the way, is that a good idea? For example, I had a global variable that checks for IE: CODE // cunning browser detect, using conditional comments var x = document.getElementById("browserIE"); if (x) { var ie = 1 } ...and I encapsulated it in a function instead (to get rid of the global): CODE function ie() { // cunning browser detect, using conditional comments var x = document.getElementById("browserIE"); if (x) { return 1 } } ...and then I test for if(ie()==1) instead of testing for if(ie). |
|
|
Jul 15 2008, 07:34 AM
Post
#9
|
|
![]() Squeeze Machine ![]() Posts: 766 Joined: 13-February 08 From: Catching the squeezed drips downunder. |
No, the slowest thing to do in Javascript, meaning the largest drain on client side resources by Javascript, is a function call. Javascript is an OOP language but it is not so to the point that complete abstraction is possible -- or the best solution.
If your namespace only has one object thats properties are objects, this is okay. Speed on todays computers isn't a huge problem but why use extra resources if you don't have to? If you only need to run a function once, it's probably best not to declare it as a function and instead create an object with the settings. This will save you some bandwidth as well (it's usually shorter to compare against variables [ if (ie) ] than functions [ if (ie()) ] -------------------- |
|
|
Jul 15 2008, 08:48 AM
Post
#10
|
|
|
Squeeze Machine ![]() Posts: 682 Joined: 15-February 08 From: UK |
No, the slowest thing to do in Javascript, meaning the largest drain on client side resources by Javascript, is a function call. Javascript is an OOP language but it is not so to the point that complete abstraction is possible -- or the best solution. If your namespace only has one object thats properties are objects, this is okay. Speed on todays computers isn't a huge problem but why use extra resources if you don't have to? If you only need to run a function once, it's probably best not to declare it as a function and instead create an object with the settings. This will save you some bandwidth as well (it's usually shorter to compare against variables [ if (ie) ] than functions [ if (ie()) ] Let me check whether I've understood this correctly:
|
|
|
Jul 15 2008, 08:56 AM
Post
#11
|
|
![]() Squeeze Machine ![]() Posts: 766 Joined: 13-February 08 From: Catching the squeezed drips downunder. |
In a nutshell that is correct.
Point one I can't confirm any further than saying that having one global packed with properties and avoiding potential namespace conflicts would be a better choice. The second and third points are logical and correct respectively. -------------------- |
|
|
Jul 15 2008, 09:05 AM
Post
#12
|
|
|
Squeeze Machine ![]() Posts: 682 Joined: 15-February 08 From: UK |
Point one I can't confirm any further than saying that having one global packed with properties and avoiding potential namespace conflicts would be a better choice. So that one global could also be something other than an object? Excuse my denseness; I'm picking up the terminology as I go (I'm pretty sketchy on what an object is, having never used one). Thanks for all this advice; I always seem to learn something fundamentally new when you answer one of my threads. |
|
|
Jul 15 2008, 09:13 AM
Post
#13
|
|
![]() Squeeze Machine ![]() Posts: 766 Joined: 13-February 08 From: Catching the squeezed drips downunder. |
Well an object is just the 'whole'.. It can be made up of properties and methods.
The global would either be an object or an array of objects or array of variables -- the same principle really; storing all global data under one namespace. An object in javascript is like an object in PHP or python except that in javascript there is are more ways to create them. $ in jQuery is just an object and it stores everything it needs, all functions and properties to do everything it does. -------------------- |
|
|
Jul 15 2008, 09:21 AM
Post
#14
|
|
|
Squeeze Machine ![]() Posts: 682 Joined: 15-February 08 From: UK |
Gotcha. Thanks.
It's my own fault for never learning to program "properly"; I just picked stuff up as I went along. This is actually quite an efficient style of learning, but it does leave dangerous gaps in my knowledge. Thankfully, you help me fill in the gaps. |
|
|
Jul 15 2008, 09:26 AM
Post
#15
|
|
![]() Squeeze Machine ![]() Posts: 766 Joined: 13-February 08 From: Catching the squeezed drips downunder. |
Gotcha. Thanks. It's my own fault for never learning to program "properly"; I just picked stuff up as I went along. This is actually quite an efficient style of learning, but it does leave dangerous gaps in my knowledge. Thankfully, you help me fill in the gaps. LOL, no probelm. -------------------- |
|
|
If you found The Web Squeeze to be helpful, please donate so we can keep this site FREE, FRESH, and fortified with Web Design & Development info!
![]() ![]() |
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:
Similar Topics
| Topic Title | Replies | Topic Starter | Views | Last Action | |||
|---|---|---|---|---|---|---|---|
![]() |
2 | marSoul | 355 | 23rd February 2008 - 10:52 AM Last post by: marSoul |
|||
![]() |
2 | Jason | 186 | 3rd April 2008 - 05:57 AM Last post by: Jason |
|||
![]() |
13 | Jason | 403 | 17th April 2008 - 12:55 AM Last post by: Ryan |
|||
![]() |
2 | MikeHopley | 201 | 25th April 2008 - 09:14 AM Last post by: MikeHopley |
|||
![]() |
6 | Jason | 331 | 2nd June 2008 - 04:35 AM Last post by: Monie |
|||






Jul 14 2008, 10:20 AM













