Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8284019
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T10:56:46+00:00 2026-06-08T10:56:46+00:00

I have both PHP and HTML in one file, following the pattern: -HTML1 -PHP1

  • 0

I have both PHP and HTML in one file, following the pattern:

-HTML1
-PHP1
-HTML2
<-script>
-PHP2
<-/script>
-rest of HTML

In the PHP2 section, I call a function in PHP1 which returns an array.
I can get a value for count($arr), but I cannot print the values in the array. It simply shows as an empty string when I examine the source code in the browser.

When I copy the statements in the function from PHP1 to PHP2, everything works – I can print the values of the array.

Code follows:

<?
include('pathdata.php');
function getThePath($node1, $node2){ //pass strings

    $n1 = $matchIDtoNum[$node1]; //get int (from pathdata)
    $n2 = $matchIDtoNum[$node2];

    $res = array();
    $res[0] = $n1;
    $res[1] = $n2;

    return $res;

}


$node1='';
$node2='';
if($_GET["node1"]){
    $node1 = $_GET["node1"];
}

if($_GET["node2"]){
    $node2 = $_GET["node2"];
}

if ($node1!='' && $node2!=''){
$arr = getThePath($node1,$node2);
}


?>

<html>
<head><title>Paths</title>    
<script>

function init(){

<?    
echo ("document.getElementById('msg1').innerHTML = 'test';\n");
if($node1!='' && $node2!=''){

    //$n1 = $matchIDtoNum[$node1];
    //$n2 = $matchIDtoNum[$node2];      
    //$res = array();
    //$res[0] = $n1;
    //$res[1] = $n2;    
    //$arr=$res;

    $num = count($arr);
    $str = implode(' ', $arr); 

    echo ("document.getElementById('msg1').innerHTML = '$arr[0]';\n"); //Empty string
    echo ("document.getElementById('msg2').innerHTML = '$str';\n"); //String with one space character
    echo ("document.getElementById('msg3').innerHTML = '$num'+' '+'$node1'+' '+'$node2';\n"); //this always works
}

?>

}

</script>


</head>
<body onload="init()">
<h1>Given two Nodes, return Shortest Path</h1>
<form name="inputform" action="getpath.php" method="get">
<input type="text" name="node1" /> 
<input type="text" name="node2" /> 
<input type="submit" value="Submit" />
<input type="reset" value="Clear" />
<br/>
<p id ="msg1"></p>
<p id ="msg2"></p>
<p id ="msg3"></p>
<br/>
</form>
</body>
</html>

Any advice on where I might be going wrong?

Thanks!

Edited to Add: What worked for me was putting global $matchIDtoNum; inside the function. i.e.

function getThePath($node1,$node2){
    global $matchIDtoNum; 
    $n1 = $matchIDtoNum[$nd1];
    //etc
}

This gave the expected output for me.

Thanks to all the commenters and answerers!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T10:56:48+00:00Added an answer on June 8, 2026 at 10:56 am

    Mmm.. on this line:

    $n1 = $matchIDtoNum[$node1]; //get int (from pathdata)
    

    It seems you want to get an array value from $matchIDtoNum. You can not take a array value from outside of a function, unless you call it as a function. For example like this:

    $n1 = getMatchIDtoNum($node1); // get int (from pathdata)
    

    and on file pathdata.php there a line like this:

    function getMatchIDtoNum($arg){
          // Your code here
          return $matchIDtoNum[$arg];
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an HTML form POSTing to the following index.php: <?php require_once(/home/full/path/to/included/file.php); ?> And
I have 2 files namely: uploading.php emaillinks.php both include a file inc.php which has
I have two different PHP files that both write to the same file. Each
I have made one file say a.php. Now I want some thing like if
I have a class in both PHP and C++ to simply take a value
I'm reading some documents about both Servlets and PHP. These document have the same
I have a PHP based application that I need to work on both a
I have two PHP functions to calculate the relation between two texts. They both
Is it possible to have a PHP function that is both recursive and anonymous?
I am a web developer, have worked with PHP and .NET both. having over

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.