LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-24-2014, 05:47 AM   #1
ButterflyMelissa
Senior Member
 
Registered: Nov 2007
Location: Somewhere on my hard drive...
Distribution: Manjaro
Posts: 2,766
Blog Entries: 23

Rep: Reputation: 411Reputation: 411Reputation: 411Reputation: 411Reputation: 411
Smile PHP and structs...


Hi all,

In JavaScript, there exists this

Code:
var userPosition = 
{
  posX : 0;
  posy : 0;
}
Then, a variable can be accessed inside, so, basicaly, userPosition is a type of variable...
Does PHP have that too? I was looking fo that, but i dont think I have found it...
Sorry for a silly question and thanks for some feedback
THor
 
Old 07-24-2014, 06:33 AM   #2
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,862
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
Arrays can be used for that, eg:
Code:
$pos= array ('posX'=>0, 'posY'=>0);
printf ("You're at %d,%d\n", $pos['posX'], $pos['posY']);

Last edited by NevemTeve; 07-24-2014 at 06:34 AM.
 
1 members found this post helpful.
Old 07-24-2014, 07:06 AM   #3
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,659
Blog Entries: 4

Rep: Reputation: 3939Reputation: 3939Reputation: 3939Reputation: 3939Reputation: 3939Reputation: 3939Reputation: 3939Reputation: 3939Reputation: 3939Reputation: 3939Reputation: 3939
The construct that is available in PHP, Perl, JavaScript, and many others ... usually at the exclusion of "structured types in the 'C' or COBOL sense" ... is called a hash table. The elements are keys, and they are looked-up by name at runtime in a construct that can have any number of keys. (And, fair warning, does not notice if a mispeled key is inserted.) However, the arrangement is flexible, efficient (enough), and very powerful.

Furthermore, the nomenclature is abused. What one language calls an "array," another might call a "dictionary," and an "array" is not a "C/C++"-like thing at all. There is rarely, if ever, any means of "compile-time checking."
 
2 members found this post helpful.
Old 07-28-2014, 03:57 AM   #4
ButterflyMelissa
Senior Member
 
Registered: Nov 2007
Location: Somewhere on my hard drive...
Distribution: Manjaro
Posts: 2,766

Original Poster
Blog Entries: 23

Rep: Reputation: 411Reputation: 411Reputation: 411Reputation: 411Reputation: 411
A twist, maybe? Classes...

the class
Code:
<?php
class person
{
  var $name;
  var $age;

  function person ()
   {
    $this->name = "";
    $this->age = 0;
   }

  function setName($name)
   {
    $this->name = $name;
   }

  function setAge($age)
   {
    $this->age = $age;
   }

  function getName()
   {
    return $this->name;
   }

  function gtAge()
   {
    return $this->age;
   }
}
?>
and in the code:

Code:
include /repository/person;

$array = new Array();

// in the loop
$aPerson = new person();
$aPerson->setName($somevalue);
//ect
$array[$counter] = $aPerson;
Maybe a feasable twist?

Still testing that, dont get data OUT the other eng

Thor

Last edited by ButterflyMelissa; 07-28-2014 at 04:01 AM.
 
Old 07-30-2014, 12:02 PM   #5
Guttorm
Senior Member
 
Registered: Dec 2003
Location: Trondheim, Norway
Distribution: Debian and Ubuntu
Posts: 1,453

Rep: Reputation: 447Reputation: 447Reputation: 447Reputation: 447Reputation: 447
If you need to combine PHP and Javascript, check out JSON.stringify in Javascript and json_encode/json_decode in PHP.
 
Old 08-02-2014, 03:15 AM   #6
ButterflyMelissa
Senior Member
 
Registered: Nov 2007
Location: Somewhere on my hard drive...
Distribution: Manjaro
Posts: 2,766

Original Poster
Blog Entries: 23

Rep: Reputation: 411Reputation: 411Reputation: 411Reputation: 411Reputation: 411
...or use a simple two-dimensional array...

Sorry for the fuss gang...

Thor
 
  


Reply

Tags
php, struct



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] A question about structs in C... trist007 Programming 13 02-11-2013 07:25 AM
C++ question on Structs mrplopez1972 Programming 3 01-23-2011 11:07 PM
copy structs in c alaios Programming 10 09-10-2005 02:31 PM
static structs? simbo Programming 3 02-05-2004 04:00 AM
Self referential structs in C? MadCactus Programming 14 01-28-2004 05:29 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:39 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration