Jump to content

Declaring Variables With Math Expressions


geno11x11

Recommended Posts

I am building an php include file. At the top, declared public and private variables contain both simple values and mathematical expressions. Those that include math within the declaration, such as $a=$b+$c, throw a syntax error. The strange thing is that php takes similar variable declarations in the calling file without complaint. There are many examples of mathematical expressions within variable declarations on the web, so it appears to be a legal practice. Whether I run the include file from the calling file or by directly accessing it in a browser, essentially the same error message occurs.

 

Below I have included (1) the error message, (2) the entire include file, and (3) a portion of the main file that calls the include file. I would appreciate any thoughts or suggestions on this problem. Thanks for your help.

 

(1) The error message:

 

Parse error: syntax error, unexpected T_VARIABLE in /srv/www/htdocs/PNS/class.tablePagination.php on line 7

 

(2) Code from the include file generating the error:

 

<?php

 

class pagination

{

private $limit = 30;

private $start = 0;

private $eu = $start -0;

private $space = " - ";

$this1 = $eu - $limit;

private $back = $eu - $limit;

private $next = $eu + $limit;

private $k = $j + $limit;

 

public $beg;

public $end;

 

public function page()

{

private $eu = $start -0;

$sql="select * FROM Complex"; /////////////// WE have to find out the number of records in our table. We will use this to break the pages///////

$result = $db->query($sql);

echo mysql_error();

$nume=mysqli_num_rows($result); // $nume is the total number of rows in the table.

//echo 'Row numbers= ' . $nume;

 

$p_limit=30; // Variables set for advance paging - should be more than $limit and set to a value for which links to be breaked

 

$p_f=$_GET['p_f']; // To take care global variable if OFF

if(!($p_f > 0))

{ // This variable is set to zero for the first page

$p_f = 0;

}

 

$p_fwd=$p_f+$p_limit;

$p_back=$p_f-$p_limit;

//////////// End of variables for advance paging ///////////////

/////////////// Start the buttom links with Prev and next link with page numbers /////////////////

echo $space . $p_f . $space . $p_f . $space . $p_limit;

$sql="select * FROM Complex order by Name limit " . $j . ", " . $k; // record set for subsequent page displays

$result=$db->query($sql);

echo "<table align = 'center' " . $width . "><tr><td align='left' width='20%'>";

//if($p_f<>0){print "<a href='$page_name?start=$p_back&p_f=$p_back'><font face='Verdana' size='2'>PREV $p_limit</font></a>"; }

if($p_f<>0){print "<font face='Verdana' size='2'>PREV $p_limit</font>"; }

echo "</td><td align='left' width='20%'>";

//// if our variable $back is equal to 0 or more then only we will display the link to move back ////////

if($back >=0 and ($back >=$p_f))

{

//print "<a href='$page_name?start=$back&p_f=$p_f'><font face='Verdana' size='2'>PREV</font></a>";

print "<font face='Verdana' size='2'>PREV</font>";

}

//////////////// Let us display the page links at center. We will not display the current page as a link ///////////

echo "</td><td align=center width='20%'>";

for($i=$p_f;$i < $nume and $i<($p_f+$p_limit);$i=$i+$limit)

{

$j=$i+1;$k=$j+$limit;

if($i <> $eu)

{

$i2=$i+$p_f;

 

//echo " <a href='$page_name?start=$i&p_f=$p_f'><font face='Verdana' size='2'>Records $j - $k</font></a> ";

echo " <font face='Verdana' size='2'>Records $j - $k</font> ";

}

else { $j=$i+1;$k=$j+$limit;echo "<font face='Verdana' size='2' > Records $j - $k</font>";} /// Current page is not displayed as link

 

}

 

 

echo "</td><td align='right' width='40%'>";

///////////// If we are not in the last page then Next link will be displayed. Here we check that /////

if($this1 < $nume and $this1 <($p_f+$p_limit))

{

//print "<a href='$page_name?start=$next&p_f=$p_f'><font face='Verdana' size='2'>NEXT $p_limit</font></a>";

print "<font face='Verdana' size='2'>NEXT $p_limit</font>";

}

echo "</td><td align='right' width='40%'>";

if($p_fwd < $nume)

{

//print "<a href='$page_name?start=$p_fwd&p_f=$p_fwd'><font face='Verdana' size='2'>NEXT $p_limit</font></a>";

print "<font face='Verdana' size='2'>NEXT $p_limit</font>";

}

echo "</td></tr>";

echo "</table>";

 

$this->$beg = $j;

$this->$end = $k;

return $j;$k;

}

}

?>

 

(3) Code from the top of the calling file: - It's lengthy, so I only posted the top portion. Note the last line, which contains addition within the variable declaration. No error is generated.

 

<html>

<head>

<title>Complex Test7</title>

 

<link href="css/styles.css" rel="stylesheet" type="text/css">

<! <link href="css/styles.php" rel="stylesheet" type="text/css">

</head>

<body class="tablefonts">

 

<center>TEST7</center>

 

<?php

require ('PNS_Styles.php');

require ('class.dbConnect.php');

require ('class.tablePagination.php');

$db=' ';

$result = "";

$empty = "<td><center><b> --- Empty --- </b></td></center>";

$page_name="complexTest6.php"; // If you use this code with a different page ( or file ) name then change this

//$limit = 30; // No of records to be shown per page.

$i=1;

$j=0;

$p_f=0;

$k=$j+$limit;

Edited by Geno
Link to comment
Share on other sites

Four days, 28 views, and no replies...

 

The obvious workaround is to break each statement into two - define the variable and follow it with the math expression assignment. But that nearly doubles the code, it is redundant, and it is inelegant IMO. Should I just chalk it up to a limitation to PHP and move on?

 

This forum is about learning and improving our skills, right folks?

 

Anyone?

Link to comment
Share on other sites

I think the key issue is where you are defining the variables -- as properties within a class. From the documentation (http://php.net/manual/en/language.oop5.properties.php):

 

"This declaration may include an initialization, but this initialization must be a constant value--that is, it must be able to be evaluated at compile time and must not depend on run-time information in order to be evaluated."

 

In short, you can assign a class property a specific value, but it can't be based on another variable -- at least, not where you are currently defining that property within the class.

 

An alternate way around this would be to the setting of values within the __construct() method:

 

class pagination 
{
private $limit = 30;
private $start = 0;
private $eu;
private $space;
private $this1;
private $back;
private $next;
private $k;

public $beg;
public $end;

function __construct($persons_name) {           
     $this->eu = $this->start - 0;
     $this->this1 = $this->eu - $this->limit;
     ...etc...
}

Link to comment
Share on other sites

Thank you Ben,

 

At very least, that tells me there is a rule that controls what I tried to do and explains why it would not work.

 

My solution, in the interim, was to keep the declarations with the class and place the assignments within the function -- and that works. Is there an advantage using the __constructs method over my solution? It doesn't look any shorter, but I am working to update my coding technique to mysqli, the use of classes, etc. So if that would fall into the auspices of better coding, I will incorporate it as well.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...