Jump to content

Recommended Posts

The first PHP script I've written looks like this:[code]<!DOCTYPE html PUBLIC
  "-//W3C//DTD XHTML 1.0 Strict//EN"
  "http:www.w3.org/TR/xhtml/DTD/xhtml1-strict.dtd">
<html>
<head>
<title>Listing 4.1 Testing the tpe of varibable</title>
</head>
<body>
<div>
<?php
$testing; // declare without assigning
print gettype ($testing ); //NULL
print "<br />";
$testing = 5;
print gettype( $testing ); //integer
print "<br />";
$testing = "five";
print gettype( $testing ); // string
print "<br />";
$testing = 5.0
print gettype( $testing ); // double
print "<br />";
$testing = true;
print gettype( $testing ); // boolean
print "<br />";
?>
</div>
</body>[/code]When I try to execute it with my bash commandline, I get this error:[code]bash-3.00$ php firstPHPscript.php
PHP Parse error:  syntax error, unexpected T_PRINT in /home/invert/programming/firstPHPscript.php on line 21[/code] Looking at line 21, I can't spot the mistake I've made.  Can you?
Link to comment
https://forums.phpfreaks.com/topic/13617-my-first-php-scripts-wont-execute-solved/
Share on other sites

[quote author=Crayon Violent link=topic=99377.msg391321#msg391321 date=1151991695]
$testing = 5.0

you forgot the ;
[/quote]
Thanks Crayon for the quick response.  WIth your correction, the script now executes with different errors:
[code]bash-3.00$ php firstPHPscript.php     
<!DOCTYPE html PUBLIC
  "-//W3C//DTD XHTML 1.0 Strict//EN"
  "http:www.w3.org/TR/xhtml/DTD/xhtml1-strict.dtd">
<html>
<head>
<title>Listing 4.1 Testing the tpe of varibable</title>
</head>
<body>
<div>
PHP Notice:  Undefined variable:  testing in /home/invert/programming/firstPHPscript.php on line 11
PHP Notice:  Undefined variable:  testing in /home/invert/programming/firstPHPscript.php on line 12
NULL<br />integer<br />string<br />double<br />boolean<br /></div>
</body>[/code]And here is how my script renders in firefox:[code]"; $testing = 5; print gettype( $testing ); //integer print "
"; $testing = "five"; print gettype( $testing ); // string print "
"; $testing = 5.0; print gettype( $testing ); // double print "
"; $testing = true; print gettype( $testing ); // boolean print "
"; ?>[/code]
edit:sp
edit:added errors
[quote author=Crayon Violent link=topic=99377.msg391729#msg391729 date=1152071461]
umm... are you running your script on your own computer? if so, then you have to install php in order to run php files on your computer.  [/quote]I have php 4 installed.  Here is the version information:[code]phpinfo()
PHP Version => 4.4.2

System => Linux 314 2.4.31 #6 Sun Jun 5 19:04:47 PDT 2005 i686
Build Date => Feb 14 2006 16:31:30
Configure Command =>  './configure' '--prefix=/usr' '--enable-force-cgi-redirect' '--enable-fastcgi' '--enable-pcntl' '--enable-sigchild' '--sysconfdir=/etc' '--enable-discard-path' '--with-config-file-path=/etc/apache' '--enable-safe-mode' '--with-openssl' '--with-mhash' '--enable-bcmath' '--with-bz2' '--with-pic' '--enable-calendar' '--enable-ctype' '--with-gdbm' '--with-db3' '--with-imap-ssl=/usr/local/lib/c-client' '--with-imap=/usr/local/lib/c-client' '--enable-dbase' '--enable-ftp' '--with-iconv' '--with-dom' '--with-exif' '--enable-exif' '--with-gd' '--enable-gd-native-ttf' '--with-jpeg-dir=/usr' '--with-png' '--with-gmp' '--enable-mbstring' '--with-curl=/usr' '--with-pcre-regex=/usr' '--with-mysql=shared,/usr' '--with-gettext=shared,/usr' '--with-expat-dir=/usr' '--with-xml' '--enable-wddx' '--with-mm=/usr' '--enable-trans-sid' '--enable-shmop' '--enable-sockets' '--with-regex=php' '--enable-sysvsem' '--enable-sysvshm' '--enable-yp' '--enable-memory-limit' '--with-tsrm-pthreads' '--enable-shared' '--disable-debug' '--with-zlib=/usr'
Server API => Command Line Interface
Virtual Directory Support => disabled
Configuration File (php.ini) Path => /etc/apache/php.ini
PHP API => 20020918
PHP Extension => 20020429
Zend Extension => 20050606
Debug Build => no
Zend Memory Manager => enabled
Thread Safety => disabled
Registered PHP Streams => php, http, ftp, https, ftps, compress.bzip2, compress.zlib 


This program makes use of the Zend Scripting Language Engine:
Zend Engine v1.3.0, Copyright (c) 1998-2004 Zend Technologies


_______________________________________________________________________


Configuration

PHP Core

Directive => Local Value => Master Value
allow_call_time_pass_reference => Off => Off
allow_url_fopen => On => On
always_populate_raw_post_data => Off => Off
arg_separator.input => & => &
arg_separator.output => & => &
asp_tags => Off => Off
auto_append_file => no value => no value
auto_prepend_file => no value => no value
browscap => no value => no value
default_charset => no value => no value
default_mimetype => text/html => text/html
define_syslog_variables => Off => Off
disable_classes => no value => no value
disable_functions => no value => no value
display_errors => Off => Off
display_startup_errors => Off => Off
doc_root => no value => no value
docref_ext => no value => no value
docref_root => no value => no value
enable_dl => On => On
error_append_string => no value => no value
error_log => no value => no value
error_prepend_string => no value => no value
error_reporting => 2047 => 2047
expose_php => On => On
extension_dir => /usr/lib/php/extensions/ => /usr/lib/php/extensions/
file_uploads => On => On
gpc_order => GPC => GPC
highlight.bg => #FFFFFF => #FFFFFF
highlight.comment => #FF8000 => #FF8000
highlight.default => #0000BB => #0000BB
highlight.html => #000000 => #000000
highlight.keyword => #007700 => #007700
highlight.string => #DD0000 => #DD0000
html_errors => Off => On
ignore_repeated_errors => Off => Off
ignore_repeated_source => Off => Off
ignore_user_abort => Off => Off
implicit_flush => On => Off
include_path => .:/usr/lib/php => .:/usr/lib/php
log_errors => On => On
log_errors_max_len => 1024 => 1024
magic_quotes_gpc => Off => Off
magic_quotes_runtime => Off => Off
magic_quotes_sybase => Off => Off
max_execution_time => 0 => 30
max_input_time => 60 => 60
memory_limit => 8M => 8M
open_basedir => no value => no value
output_buffering => 0 => 4096
output_handler => no value => no value
post_max_size => 8M => 8M
precision => 14 => 14
register_argc_argv => On => Off
register_globals => Off => Off
report_memleaks => On => On
safe_mode => Off => Off
safe_mode_exec_dir => no value => no value
safe_mode_gid => Off => Off
safe_mode_include_dir => no value => no value
sendmail_from => no value => no value
sendmail_path => /usr/sbin/sendmail -t -i  => /usr/sbin/sendmail -t -i
serialize_precision => 100 => 100
short_open_tag => On => On
SMTP => localhost => localhost
smtp_port => 25 => 25
sql.safe_mode => Off => Off
track_errors => Off => Off
unserialize_callback_func => no value => no value
upload_max_filesize => 2M => 2M
upload_tmp_dir => no value => no value
user_dir => no value => no value
variables_order => GPCS => GPCS
xmlrpc_error_number => 0 => 0
xmlrpc_errors => Off => Off
y2k_compliance => On => On

bcmath

BCMath support => enabled

bz2

BZip2 Support => Enabled
BZip2 Version => 1.0.3, 15-Feb-2005

calendar

Calendar support => enabled

ctype

ctype functions => enabled

curl

CURL support => enabled
CURL Information => libcurl/7.12.2 OpenSSL/0.9.7g zlib/1.2.3 libidn/0.5.17

dba

DBA support => enabled
Supported handlers => gdbm cdb cdb_make db3 inifile flatfile

domxml

DOM/XML => enabled
DOM/XML API Version => 20020815
libxml Version => 20622
HTML Support => enabled
XPath Support => enabled
XPointer Support => enabled

exif

EXIF Support => enabled
EXIF Version => 1.4 $Id: exif.c,v 1.118.2.37.2.4 2006/01/01 13:46:52 sniper Exp $
Supported EXIF Version => 0220
Supported filetypes => JPEG,TIFF

ftp

FTP support => enabled

gd

GD Support => enabled
GD Version => bundled (2.0.28 compatible)
GIF Read Support => enabled
GIF Create Support => enabled
JPG Support => enabled
PNG Support => enabled
WBMP Support => enabled
XBM Support => enabled

gettext

GetText Support => enabled

gmp

gmp support => enabled

iconv

iconv support => enabled
iconv implementation => glibc
iconv library version => 2.3.6

Directive => Local Value => Master Value
iconv.input_encoding => ISO-8859-1 => ISO-8859-1
iconv.internal_encoding => ISO-8859-1 => ISO-8859-1
iconv.output_encoding => ISO-8859-1 => ISO-8859-1

imap

IMAP c-Client Version => 2004
SSL Support => enabled

mbstring

Multibyte Support => enabled
Japanese support => enabled
Simplified chinese support => enabled
Traditional chinese support => enabled
Korean support => enabled
Russian support => enabled
Multibyte (japanese) regex support => enabled

                                        mbstring extension makes use of "streamable kanji code filter and converter", which is distributed under the GNU Lesser General Public License version 2.1.                                       

Directive => Local Value => Master Value
mbstring.detect_order => no value => no value
mbstring.encoding_translation => Off => Off
mbstring.func_overload => 0 => 0
mbstring.http_input => pass => pass
mbstring.http_output => pass => pass
mbstring.internal_encoding => ISO-8859-1 => no value
mbstring.language => neutral => neutral
mbstring.substitute_character => no value => no value

mhash

MHASH support => Enabled
MHASH API Version => 20020524

mysql

MySQL Support => enabled
Active Persistent Links => 0
Active Links => 0
Client API version => 4.1.20
MYSQL_MODULE_TYPE => external
MYSQL_SOCKET => /var/run/mysql/mysql.sock
MYSQL_INCLUDE => -I/usr/include/mysql
MYSQL_LIBS => -L/usr/lib -lmysqlclient

Directive => Local Value => Master Value
mysql.allow_persistent => On => On
mysql.connect_timeout => 60 => 60
mysql.default_host => no value => no value
mysql.default_password => no value => no value
mysql.default_port => no value => no value
mysql.default_socket => no value => no value
mysql.default_user => no value => no value
mysql.max_links => Unlimited => Unlimited
mysql.max_persistent => Unlimited => Unlimited
mysql.trace_mode => Off => Off

openssl

OpenSSL support => enabled
OpenSSL Version => OpenSSL 0.9.7g 11 Apr 2005

overload

User-Space Object Overloading Support => enabled

pcntl

pcntl support => enabled

pcre

PCRE (Perl Compatible Regular Expressions) Support => enabled
PCRE Library Version => 6.4 05-Sep-2005

posix

Revision => $Revision: 1.51.2.4.2.1 $

session

Session Support => enabled
Registered save handlers => files user mm

Directive => Local Value => Master Value
session.auto_start => Off => Off
session.bug_compat_42 => Off => Off
session.bug_compat_warn => On => On
session.cache_expire => 180 => 180
session.cache_limiter => nocache => nocache
session.cookie_domain => no value => no value
session.cookie_lifetime => 0 => 0
session.cookie_path => / => /
session.cookie_secure => Off => Off
session.entropy_file => no value => no value
session.entropy_length => 0 => 0
session.gc_divisor => 1000 => 1000
session.gc_maxlifetime => 1440 => 1440
session.gc_probability => 1 => 1
session.name => PHPSESSID => PHPSESSID
session.referer_check => no value => no value
session.save_handler => files => files
session.save_path => /tmp => /tmp
session.serialize_handler => php => php
session.use_cookies => On => On
session.use_only_cookies => Off => Off
session.use_trans_sid => Off => Off

shmop

shmop support => enabled

sockets

Sockets Support => enabled

standard

Regex Library => Bundled library enabled
Dynamic Library Support => enabled
Path to sendmail => /usr/sbin/sendmail -t -i

Directive => Local Value => Master Value
assert.active => 1 => 1
assert.bail => 0 => 0
assert.callback => no value => no value
assert.quiet_eval => 0 => 0
assert.warning => 1 => 1
auto_detect_line_endings => 0 => 0
default_socket_timeout => 60 => 60
safe_mode_allowed_env_vars => PHP_ => PHP_
safe_mode_protected_env_vars => LD_LIBRARY_PATH => LD_LIBRARY_PATH
url_rewriter.tags => a=href,area=href,frame=src,input=src,form=fakeentry => a=href,area=href,frame=src,input=src,form=fakeentry
user_agent => no value => no value

tokenizer

Tokenizer Support => enabled

wddx

WDDX Support => enabled
WDDX Session Serializer => enabled

xml

XML Support => active
XML Namespace Support => active
EXPAT Version => expat_1.95.8

yp

YP Support => enabled

zlib

ZLib Support => enabled
Compiled Version => 1.2.3
Linked Version => 1.2.3

Directive => Local Value => Master Value
zlib.output_compression => Off => Off
zlib.output_compression_level => -1 => -1
zlib.output_handler => no value => no value

Additional Modules

Module Name
dbase
sysvsem
sysvshm

Environment

Variable => Value
CPLUS_INCLUDE_PATH => /usr/lib/qt/include
MANPATH => /usr/local/man:/usr/man:/usr/X11R6/man:/usr/lib/java/man:/opt/kde/man:/usr/lib/qt/doc/man:/usr/share/texmf/man
HZ => 100
HOSTNAME => 314.slackware
SHELL => /bin/bash
TERM => xterm
WINDOWID => 25165826
HUSHLOGIN => FALSE
QTDIR => /usr/lib/qt
USER => invert
LS_COLORS => no=00:fi=00:di=01;34:ln=01;36:pi=40;33:so=01;35:bd=40;33;01:cd=40;33;01:or=40;31;01:ex=01;32:*.bat=01;32:*.BAT=01;32:*.btm=01;32:*.BTM=01;32:*.cmd=01;32:*.CMD=01;32:*.com=01;32:*.COM=01;32:*.dll=01;32:*.DLL=01;32:*.exe=01;32:*.EXE=01;32:*.arj=01;31:*.bz2=01;31:*.deb=01;31:*.gz=01;31:*.lzh=01;31:*.rpm=01;31:*.tar=01;31:*.taz=01;31:*.tb2=01;31:*.tbz2=01;31:*.tbz=01;31:*.tgz=01;31:*.tz2=01;31:*.z=01;31:*.Z=01;31:*.zip=01;31:*.ZIP=01;31:*.zoo=01;31:*.asf=01;35:*.ASF=01;35:*.avi=01;35:*.AVI=01;35:*.bmp=01;35:*.BMP=01;35:*.flac=01;35:*.FLAC=01;35:*.gif=01;35:*.GIF=01;35:*.jpg=01;35:*.JPG=01;35:*.jpeg=01;35:*.JPEG=01;35:*.m2a=01;35:*.M2a=01;35:*.m2v=01;35:*.M2V=01;35:*.mov=01;35:*.MOV=01;35:*.mp3=01;35:*.MP3=01;35:*.mpeg=01;35:*.MPEG=01;35:*.mpg=01;35:*.MPG=01;35:*.ogg=01;35:*.OGG=01;35:*.ppm=01;35:*.rm=01;35:*.RM=01;35:*.tga=01;35:*.TGA=01;35:*.tif=01;35:*.TIF=01;35:*.wav=01;35:*.WAV=01;35:*.wmv=01;35:*.WMV=01;35:*.xbm=01;35:*.xpm=01;35:
GDK_USE_XFT => 1
KDEDIR => /opt/kde
T1LIB_CONFIG => /usr/share/t1lib/t1lib.config
MINICOM => -c on
MAIL => /var/spool/mail/invert
PATH => /usr/local/bin:/usr/bin:/bin:/usr/X11R6/bin:/usr/games:/opt/www/htdig/bin:/usr/lib/java/bin:/usr/lib/java/jre/bin:/opt/kde/bin:/usr/lib/qt/bin:/usr/share/texmf/bin:.
LC_COLLATE => C
INPUTRC => /etc/inputrc
PWD => /home/invert
JAVA_HOME => /usr/lib/java
LANG => en_US
COLORFGBG => 0;default;15
SHLVL => 4
HOME => /home/invert
LS_OPTIONS =>  --color=auto -F -b -T 0
LESS => -M
LOGNAME => invert
LESSOPEN => |lesspipe.sh %s
DISPLAY => :0.0
COLORTERM => rxvt-xpm
XAUTHORITY => /home/invert/.Xauthority
_ => /usr/bin/php

PHP Variables

Variable => Value
_SERVER["CPLUS_INCLUDE_PATH"] => /usr/lib/qt/include
_SERVER["MANPATH"] => /usr/local/man:/usr/man:/usr/X11R6/man:/usr/lib/java/man:/opt/kde/man:/usr/lib/qt/doc/man:/usr/share/texmf/man
_SERVER["HZ"] => 100
_SERVER["HOSTNAME"] => 314.slackware
_SERVER["SHELL"] => /bin/bash
_SERVER["TERM"] => xterm
_SERVER["WINDOWID"] => 25165826
_SERVER["HUSHLOGIN"] => FALSE
_SERVER["QTDIR"] => /usr/lib/qt
_SERVER["USER"] => invert
_SERVER["LS_COLORS"] => no=00:fi=00:di=01;34:ln=01;36:pi=40;33:so=01;35:bd=40;33;01:cd=40;33;01:or=40;31;01:ex=01;32:*.bat=01;32:*.BAT=01;32:*.btm=01;32:*.BTM=01;32:*.cmd=01;32:*.CMD=01;32:*.com=01;32:*.COM=01;32:*.dll=01;32:*.DLL=01;32:*.exe=01;32:*.EXE=01;32:*.arj=01;31:*.bz2=01;31:*.deb=01;31:*.gz=01;31:*.lzh=01;31:*.rpm=01;31:*.tar=01;31:*.taz=01;31:*.tb2=01;31:*.tbz2=01;31:*.tbz=01;31:*.tgz=01;31:*.tz2=01;31:*.z=01;31:*.Z=01;31:*.zip=01;31:*.ZIP=01;31:*.zoo=01;31:*.asf=01;35:*.ASF=01;35:*.avi=01;35:*.AVI=01;35:*.bmp=01;35:*.BMP=01;35:*.flac=01;35:*.FLAC=01;35:*.gif=01;35:*.GIF=01;35:*.jpg=01;35:*.JPG=01;35:*.jpeg=01;35:*.JPEG=01;35:*.m2a=01;35:*.M2a=01;35:*.m2v=01;35:*.M2V=01;35:*.mov=01;35:*.MOV=01;35:*.mp3=01;35:*.MP3=01;35:*.mpeg=01;35:*.MPEG=01;35:*.mpg=01;35:*.MPG=01;35:*.ogg=01;35:*.OGG=01;35:*.ppm=01;35:*.rm=01;35:*.RM=01;35:*.tga=01;35:*.TGA=01;35:*.tif=01;35:*.TIF=01;35:*.wav=01;35:*.WAV=01;35:*.wmv=01;35:*.WMV=01;35:*.xbm=01;35:*.xpm=01;35:
_SERVER["GDK_USE_XFT"] => 1
_SERVER["KDEDIR"] => /opt/kde
_SERVER["T1LIB_CONFIG"] => /usr/share/t1lib/t1lib.config
_SERVER["MINICOM"] => -c on
_SERVER["MAIL"] => /var/spool/mail/invert
_SERVER["PATH"] => /usr/local/bin:/usr/bin:/bin:/usr/X11R6/bin:/usr/games:/opt/www/htdig/bin:/usr/lib/java/bin:/usr/lib/java/jre/bin:/opt/kde/bin:/usr/lib/qt/bin:/usr/share/texmf/bin:.
_SERVER["LC_COLLATE"] => C
_SERVER["INPUTRC"] => /etc/inputrc
_SERVER["PWD"] => /home/invert
_SERVER["JAVA_HOME"] => /usr/lib/java
_SERVER["LANG"] => en_US
_SERVER["COLORFGBG"] => 0;default;15
_SERVER["SHLVL"] => 4
_SERVER["HOME"] => /home/invert
_SERVER["LS_OPTIONS"] =>  --color=auto -F -b -T 0
_SERVER["LESS"] => -M
_SERVER["LOGNAME"] => invert
_SERVER["LESSOPEN"] => |lesspipe.sh %s
_SERVER["DISPLAY"] => :0.0
_SERVER["COLORTERM"] => rxvt-xpm
_SERVER["XAUTHORITY"] => /home/invert/.Xauthority
_SERVER["_"] => /usr/bin/php
_SERVER["PHP_SELF"] =>
_SERVER["SCRIPT_NAME"] =>
_SERVER["SCRIPT_FILENAME"] =>
_SERVER["PATH_TRANSLATED"] =>
_SERVER["DOCUMENT_ROOT"] =>
_SERVER["argv"] => Array
(
)

_SERVER["argc"] => 0

PHP License
This program is free software; you can redistribute it and/or modify
it under the terms of the PHP License as published by the PHP Group
and included in the distribution in the file:  LICENSE

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

If you did not receive a copy of the PHP license, or have any
questions about PHP licensing, please contact license@php.net.[/code]

edit:sp
My second script in PHP is a settype exercise.  It looks like this:[code]<!DOCTYPE html PUBLIC
"-//W3C// DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml/DTD/xhtml1-strict.dtd">
<html>
<head>
<title>Listing 4.2 Changing the Type of a variable with settype()</title>
</head>
</html>
<body>
<div>
<?php
$undecided = 3.14;
print gettype( $undecided ); // double
print "-- $undecided<br />"; //3.14
settype( $undecided, string );
print gettype( $undecided ); // string
print "-- $undecided<br />"; //3.14
settype( $undecided, int );
print gettype( $undecided ); // integer
print " -- $undecided<br />"; // 3
settype( $undecided, double );
print gettype( $undecided<br />"; // double
print " -- $undecided<br />"; // 3.0
settype( $undecided, bool );
print gettype( $undecided ); //boolean
print " -- $undecided<br />; // 1
?>
,/div>
</body>
</html>[/code] That script produces this error:[code]bash-3.1$ php settypePHPExercise.php
PHP Parse error:  syntax error, unexpected '>' in /home/invert/programming/settypePHPExercise.php on line 22
bash-3.1$ [/code] I can't figure out what's wrong with my line 22. Can you?

Yes, the </div> tag is incorrect, but PHP shouldn't be complaining about it. What's really wrong is that you forgot the terminating double quote on this line:
[code]<?php print " -- $undecided<br />; // 1?>[/code]
It should be written:
[code]<?php print " -- $undecided<br />"; // 1?>[/code]

Ken
According to itrebal on freenode, “well $testing; doesn't declare the variable - you avhe to actually set it to something.”  Further, “you cant output HTML with the variable like that.” I didn’t really know what itrebal was talking about so I asked him, “wtf?”  itrebal recommended http://ca3.php.net/strings and http://us3.php.net/manual/en/language.types.string.php

I'll respond to the the posts made by kenrbnsn and Dville soon.
[quote author=kenrbnsn link=topic=99377.msg394900#msg394900 date=1152589782]
Yes, the </div> tag is incorrect, but PHP shouldn't be complaining about it. What's really wrong is that you forgot the terminating double quote on this line:
[code]<?php print " -- $undecided<br />; // 1?>[/code]
It should be written:
[code]<?php print " -- $undecided<br />"; // 1?>[/code][/quote]
[quote author=Dville link=topic=99377.msg394875#msg394875 date=1152585537]
[code],/div>[/code]

should be

[code]</div>[/code]Ken

[/quote]Thanks for the quick replies.  But even with these two corrections, line 22 is still some how problematic.

This following line is incorrect:

[code]print gettype( $undecided<br />"; // double
[/code]

Change it to:

[code]print gettype( $undecided<br />); // double
[/code]

Notice that you have a quote at the end just before the semi-colon that is supposed to be a closing parenthesis.
Thanks bodenzord for the parenthesis correction.  I'm still getting an error for line 22.  Here is what my script looks like now: [code]<!DOCTYPE html PUBLIC
"-//W3C// DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml/DTD/xhtml1-strict.dtd">
<html>
<head>
<title>Listing 4.2 Changing the Type of a variable with settype()</title>
</head>
</html>
<body>
<div>
<?php
$undecided = 3.14;
print gettype( $undecided ); // double
print "-- $undecided<br />"; //3.14
settype( $undecided, string );
print gettype( $undecided ); // string
print "-- $undecided<br />"; //3.14
settype( $undecided, int );
print gettype( $undecided ); // integer
print " -- $undecided<br />"; // 3
settype( $undecided, double );
print gettype( $undecided<br />); // double
print " -- $undecided<br />"; // 3.0
settype( $undecided, bool );
print gettype( $undecided ); //boolean
print " -- $undecided<br />"; // 1
?>
</div>
</body>
</html>[/code]
Okay...the problem was that you had <br /> on line 22 that should not have been there. Check out the corrected code:

[code]<!DOCTYPE html PUBLIC
"-//W3C// DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml/DTD/xhtml1-strict.dtd">
<html>
<head>
<title>Listing 4.2 Changing the Type of a variable with settype()</title>
</head>
</html>
<body>
<div>
<?php
$undecided = 3.14;
print gettype( $undecided ); // double
print "-- $undecided<br />"; //3.14
settype( $undecided, string );
print gettype( $undecided ); // string
print "-- $undecided<br />"; //3.14
settype( $undecided, int );
print gettype( $undecided ); // integer
print " -- $undecided<br />"; // 3
settype( $undecided, double );
print gettype( $undecided); // double
print " -- $undecided<br />"; // 3.0
settype( $undecided, bool );
print gettype( $undecided ); //boolean
print " -- $undecided<br />"; // 1
?>
</div>
</body>
</html>[/code]
[quote author=bodenzord link=topic=99377.msg394950#msg394950 date=1152596732]
Okay...the problem was that you had <br /> on line 22 that should not have been there. Check out the corrected code:

[code]<!DOCTYPE html PUBLIC
"-//W3C// DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml/DTD/xhtml1-strict.dtd">
<html>
<head>
<title>Listing 4.2 Changing the Type of a variable with settype()</title>
</head>
</html>
<body>
<div>
<?php
$undecided = 3.14;
print gettype( $undecided ); // double
print "-- $undecided<br />"; //3.14
settype( $undecided, string );
print gettype( $undecided ); // string
print "-- $undecided<br />"; //3.14
settype( $undecided, int );
print gettype( $undecided ); // integer
print " -- $undecided<br />"; // 3
settype( $undecided, double );
print gettype( $undecided); // double
print " -- $undecided<br />"; // 3.0
settype( $undecided, bool );
print gettype( $undecided ); //boolean
print " -- $undecided<br />"; // 1
?>
</div>
</body>
</html>[/code]
[/quote]
Uhm, when I run bodenzord's modified script, I get this:

[code]bash-3.1$ php firstPHPscript-bodenzord.php
<!DOCTYPE html PUBLIC
"-//W3C// DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml/DTD/xhtml1-strict.dtd">
<html>
<head>
<title>Listing 4.2 Changing the Type of a variable with settype()</title>
</head>
</html>
<body>
<div>
double-- 3.14<br />PHP Notice:  Use of undefined constant string - assumed 'string' in /home/invert/programming/PHP/firstPHPscript-bodenzord.php on line 15
string-- 3.14<br />PHP Notice:  Use of undefined constant int - assumed 'int' in /home/invert/programming/PHP/firstPHPscript-bodenzord.php on line 18
integer -- 3<br />PHP Notice:  Use of undefined constant double - assumed 'double' in /home/invert/programming/PHP/firstPHPscript-bodenzord.php on line 21
double -- 3<br />PHP Notice:  Use of undefined constant bool - assumed 'bool' in /home/invert/programming/PHP/firstPHPscript-bodenzord.php on line 24
boolean -- 1<br /></div>
</body>
</html>bash-3.1$ [/code]What's going on here? What am I doing wrong?

edit:sp
[code]<?php
$testing; // declare without assigning
print gettype ($testing ); //NULL
print "<br />";
$testing = 5;
print gettype( $testing ); //integer
print "<br />";
$testing = "five";
print gettype( $testing ); // string
print "<br />";
$testing = 5.0
print gettype( $testing ); // double
print "<br />";
$testing = true;
print gettype( $testing ); // boolean
print "<br />";
?>[/code]

print sucks for one, use echo.. for 2, you forgot a semicolon, 3, all that unneeded whitespace adds to your filesize, get rid of it..

[code]
<?php
$testing = ""; // declare without assigning
echo gettype($testing); //NULL
echo "<br>";
$testing = 5;
echo gettype($testing); //integer
echo "<br>";
$testing = "five";
echo gettype($testing); // string
echo "<br>";
$testing = 5.0;
echo gettype($testing); // double
echo "<br>";
$testing = true;
echo gettype($testing); // boolean
echo "<br>";
/* returns:
string<br>integer<br>string<br>double<br>boolean<br>
*/
?>
[/code]
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  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.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.