简体   繁体   中英

how do i get another variable from different php file into another file?

I listed all Country names in "countries.php", like:

<?php
$countryArray = array(
    'AD'=>array('name'=>'ANDORRA','code'=>'376'),
    'AE'=>array('name'=>'UNITED ARAB EMIRATES','code'=>'971'), ...
);
function countrySelector($defaultCountry = "", $id = "", $name = "", $classes = ""){
    global $countryArray; // Assuming the array is placed above this function

    $output = "<select id='".$id."' name='".$name."' class='". $classes."'>";
    $output .= "</select>";
}

using the file in "signup.php", well this works but, what I want to do is to create input (mobile number) in "signup.php", In the mobile input, I want to use a country code when a user chooses a country, It will insert the country code into mobile input. Like: user chose US +1.. in the other input it will show +1(and the user can type their own number without adding the country-code by their self) I have also done the mobile validation properly. I have no idea how to do the code. please help.

You should read up on Classes. If you define your script in a class you access the variables by first calling use myClass and then accessing the variable statically or by creating an instance of your class object.

For example here you should create Country class and have your $countryArray as a static variable in that class that you could access using Country::$countryArray

Here's how your Country class would look:

class Country { 
    public $countryArray = ['usa', 'canada']; 
} 

Then in your other script you could like so:

use Country;

$countries = Country::$countryArray;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM